lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On 28/01/11 13:07, Roberto Ierusalimschy wrote:
[...]
> In Lua, we favor generic and powerful constructs. Actually, a 'goto'
> would be almost perfect.  But its interaction with variable scoping
> is quite confusing in a language with first-class functions like Lua.
> Labeled breaks seem the next best thing, as it allows any forward goto
> that does not enter a variable scope.

Personally I think 'goto' fits very nicely within the Lua ethos of
providing mechanism, not policy, but I think people already know my
stance on this...

My particular use case is to be able to do arbitrary jumping between
points within a single function --- backwards as well as forwards. I'm a
little leery of the labelled break concept because it doesn't support
jumping backwards.

It's certainly possible to do perpetrate something like this:

while true do :scope0_e: do :scope0_c:
	if fn1() then break :scope0_e: end
	if fn2() then break :scope0_c: end
	while true do :scope1_e: do :scope1_c:
		if fn3() then break :scope0_e: end
		if fn4() then break :scope0_c: end
		if fn5() then break :scope1_e: end
		if fn6() then break :scope1_c: end
		break
	end
	break
end

A break to *_e jumps to the end of the scope, a break to *_c jumps to
the beginning of the scope.

To my eye that sort of thing is even harder to read and more evil than
an honest goto, plus I don't believe it will work in all cases --- I
don't think that forward jumps across targets of backward jumps can be
done with this idiom.

-- 
┌─── dg@cowlark.com ───── http://www.cowlark.com ─────
│
│ life←{ ↑1 ⍵∨.^3 4=+/,¯1 0 1∘.⊖¯1 0 1∘.⌽⊂⍵ }
│ --- Conway's Game Of Life, in one line of APL

Attachment: signature.asc
Description: OpenPGP digital signature