[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Named loops and blocks for use with break
- From: Rici Lake <lua@...>
- Date: Thu, 6 Oct 2005 08:39:06 -0500
On 6-Oct-05, at 5:59 AM, Doug Rogers wrote:
Thank you for sharing your patch. I like it, though I think some of the
iteration controls are hard to read - especially the ones that perform
a
'continue' operation. With time I'm sure I would grow more comfortable
with them. I'd prefer 'break when' and 'continue when'.
Well, you're used to 'break' and 'continue'. But those are very
difficult to explain to novice programmers, particularly 'continue'.
Indeed, I think many programmers feel just a little uncomfortable with
'continue' because they don't really understand its full semantics, and
hence avoid using it.
Shivers' vocabulary strikes me as well thought-out, at least for
English language speakers.
Here's the little example, again:
function command_shell(stream, handlers, endword)
for line in stream:lines() do
until line == endword
unless line = line:gsub("^%s*", "")
satisfies line == "" or line:find("^#")
local cmd, rest = line:match("(%S+)%s*(.*)")
if h = handlers[cmd] satisfies h then
h(argsplit(cmd, rest))
else
print("Unrecognized command "..cmd)
end
end
end
Here, "until" is pretty clear: stop looping if the condition is true. I
think "unless" is also pretty clear: don't do the rest of this loop if
the condition is true. (I'm not sure that adding the satisfies clause
is any improvement to the original, but it was free so I thought I'd
try it.)
Since the guards are written in strict linear order, they are easy to
explain. The loop always starts at the top and works down to the end.
Contrast that with the confusion over whether 'continue' does or does
not execute the tailguard in "repeat ... continue ... until cond"
But maybe that's just my perspective.
On the other hand, they don't read well if you want to terminate a
surrounding loop.
The 'when'
keyword could then be used to support a 'case' statement
implementation.
And now we're off on another grand exploration!