[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: lexical goto would include full continuations?
- From: Charles Engelhard <ochuckles@...>
- Date: Mon, 11 Jul 2011 19:34:21 -0700
In the current 5.2 spec, it mentions that goto cannot see labels through a nested function body. I think this is unfortunate for a few reasons and hope that the eventual goal is to extend the functionality. There are two problems with this restriction:
- It's un-Lua-ish, in that suddenly there's a place in the language where function scopes are special. It's a pretty surprising restriction, unless you think in terms of the admittedly difficult implementation issues.
- It's a missed opportunity to have the power of Scheme-style continuations with an understandable interface. I've always thought that this feature of Scheme failed to spread as much as other features that Scheme introduced simply because call-with-current-continuation, as an interface to the feature, tends to confuse people. A quick proof that a proper lexical goto would implement continuations is as follows:
function callcc(fun)
local ret
::exit::
if ret then
return table.unpack(ret)
else
return fun(function(...)
ret = {...}
goto exit
end)
end
end
(Note that "goto exit" requires visibility of the label through a nested function body.)
(Actually, this undocumented bit of code is a more understandable explanation of how call/cc works than any I've ever seen!)
Technically, this addition to the language would also mean that the coroutine library could be implemented in pure Lua! This might not be a good idea for performance reasons (depending on implementations) but is another good argument for the power of the feature. Similarly, it would allow for all sorts of non-local exits, such as exceptions.
Lua has been in a state of "almost having continuations" for a while, and I think it's about time to bring them in. A lexical goto would be a novel, straightforward, and unsurprising way of doing this.