lua-users home
lua-l archive

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


On Sat, Nov 20, 2010 at 1:46 AM, David Manura <dm.lua@math2.org> wrote:
> On Fri, Nov 19, 2010 at 2:20 AM, Axel Kittenberger <axkibe@gmail.com> wrote:
>> wait, +0 is something different than -0? No please not.
>
> Another numbering scheme may be better, but I think now that numbered
> scopes are less readable/decipherable than named scopes.
> Interestingly, Lua already has named scopes: they are called lexical
> variables.  That leads to this proposal:
>
>  for x=1,10 do
>    local cont  -- dummy variable
>    for y=1,10 do
>      if f(x,y) then break x end    -- jump to end of x scope (i.e.
> break outer loop)
>      if g(x,y) then break cont end   -- jump to end of cont scope
> (i.e. continue outer loop)
>    end
>  end
>
> which may warrant serious consideration.  It opens up some other
> possibilities too, if the language designer chooses to allow it:
>
>  do
>    local s
>    if a() then break s end   -- break out of non-loop blocks too.
>  end
>
>  function f()
>    local x
>    return function()
>      break x  -- could this even be allowed? (break across functions)
>    end
>  end
>  f()()()()()
>

What about  "tagged blocks" as an alternative to goto. (similar to Ada exit)?
You can use with any command that creates a block - if, while, repeat,
for, do or even a function.
and with the end to inform the compiler what it closes (avoids
inconsistent end comments) .

do :regular -- may follows while, repeat, for etc.
  while cond1 do
    if cond2 then break :regular end -- or exit :regular (I prefer)
  end
  return
end :regular
- error handling

or

if :test1 cond then
  if   cond2 then
    stm1;
    stm2;
    if cond3 then break :test1 end -- exit :test1;
    stm4;
  else
    stm5
  end
end :test1

The tag in while or for could be after the do.

while cond do : tag
end :tag

or after the while

while :tag cond do
end :tag


Well, since we are talking about changes in Lua syntax, another simple
and useful change could be
the creation of "conditional jumps" that does not need of "then" and
"end" and never produces a "I forgot to create the block so a later
second command always be executed" syndrome because the second command
will never be executed after the "jump".

if cond return  x
if cond break
if cond continue

example:

while true do  -- I prefer the "loop" keyword
   -- do some initialization
  stm1
  if cond break
   -- main code
end

or

function f (val1, val2)
  if not test(val1) return 'error ...'
  -- rest of code
end

-- 
Nilson