lua-users home
lua-l archive

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


On Tue, Jul 12, 2011 at 04:34:21AM +0200, Charles Engelhard wrote:
> In the current 5.2 spec, it mentions that goto cannot see labels through a
> nested function body. 
Let's have the actual words from beta-rc7.
| A label is visible in the entire block where it is defined, except inside
| nested blocks where a label with the same name is defined and inside nested
| functions. A goto may jump to any visible label as long as it does not enter
| into the scope of a local variable. 
I presume your point is that this spec prohibits jumping from inside
a function to a label outside it.  You want to do this sort of thing:

    a,b,c = f(d,e,f)
    -- lots of other code
    ::target:: print "Naughty, naughty!"; os.exit(-1)
    
    function f(x,y,z)
        if z==nil then goto target end
    --  lots of other code
        end

> - 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.
Implementation for ordinary, Lua-ish applications is not difficult.  

You do the equivalent of:

goto_target = {}    -- unique object
a,b,c = f(d,e,f)
if a==goto_target then goto target end
-- lots of other code
::target:: print "Naughty, naughty!"; os.exit(-1)

function f(x,y,z)
    if z==nil then return goto_target end
--  lots of other code
    end

Point is, as you can see, this is dead easy in Lua itself.  It costs
three lines of code and a new table, but it gives the bonus that any
reader of the code knows, right up there where f is called, that there 
may be a jump.

Moreover, doing it this way is more powerful than lexical scoping, 
because if the call to f and the label in question are inside the
same scope of a local variable, it's still possible.

But OK, the main point of your post was that you could implement
something called call/cc if Lua had lexical calls.  I had to look 
up Wikipedia to find out that call/cc is a Scheme speciality, in which
it can be used “to emulate the functionality of the return statement …
which is missing from Scheme”.  (Random thought: why not post a request
for including a return statement on Scheme's mailing list?)

I can't understand the rest of that article; I can't even understand
your code which you claim is the most understandable explanation
you've ever seen.  So I can't check whether the construction I show 
above is be enough to enable you to implement your callcc function. 
You might be able to tell.

But is it worth while, even so?  You say:
> I've always thought that this feature of Scheme failed to spread as 
> much as other features … because … tends to confuse people. 
I can confirm that tendency first-hand.

Can't we leave call/cc to people who understand Scheme and Haskell and 
phrases like “Curry-Howard correspondence” and “intuitionistic logic”,
and just carry on our state of Lua-tic bliss, contentedly puffing on our
return statements?

Dirk