lua-users home
lua-l archive

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


On 22/06/2011 7.25, David Manura wrote:
On Tue, Jun 21, 2011 at 11:15 PM, Josh Simmons<simmons.44@gmail.com>  wrote:
"A label is visible in the entire block where it is defined (including
nested blocks, but not nested functions). A function cannot have more
than one label with a given name." Emphasis on the second sentence.

There are three parts, which I had to reread this a couple times,
probably due to the order in which it is written:

I agree, I found it a bit hard to read it too.


(1) A label is visible in the entire block where it is defined
     (including nested blocks, but not nested functions).

(2) A function cannot have more than one label with a given name.

(3) A goto may jump to any visible label as long as it does not enter
     into the scope of a local variable.

Part #2 is easy to understand.  To be more specific though, labels in
nested functions don't seem to count.

Part #3 depends on understanding part #1.  On first reading part #1,
it's not clear what "visibility" is implying (answer: nothing in
itself), especially when we then read part #2 and learn that an
invisible label can still cause a name conflict and then read part #3
and learn that there are some visible labels we cannot jump to.

Another way to say part #1+3, without using the term "visibility", is
that a goto may jump to any label directly attached to either the same
block or any block containing it up to the enclosing function,
provided the jump does not enter the scope of a local variable.

Yet another way to say it is that a goto may jump to any label inside
the containing function provided it does not enter the scope of a
block or local variable.  I like that.

Also, note: "scope of a local variable" is defined to end before any
labels at the end of a block.  This allows simulating continue: "while
f() do if g() then goto continue end; local x; ..... ; ::continue::
end" is ok.

I was just thinking of it right now. I'm not a fan of continue, but I find it really useful sometimes.

Now that the goto machinery is in place, wouldn't a "continue" keyword be useful and easily introduced? If I remember well, Lua authors weren't contrary to it in principle, but because of the incompatibilities with "until".

Now this seems to work:

--------------
local s = "apple banana kiwi pear apricot plum mango \*
papaya blackberry coconut strawberry"

local banned_letter = "p"

local word, _
local pos = 0
repeat
   _, pos, word = s:find( '(%w+)', pos + 1 )
   if word and word:sub( 1, 1 ) == banned_letter then
      goto continue  -- or simply "continue" keyword
   end
   print( word )
   -- local foobar  -- this would generate an error, if uncommented
   ::continue::  -- hidden label if continue were introduced
until word ==  nil
--------------------

So continue could be translated internally to a such a goto. That is, "continue" could be just syntactic sugar for such a goto. This solves also the problem of continue bypassing a local var declaration: this would generate an error in the same way now goto would generate one if, after the "print" above, there were a local declaration.


-- Lorenzo