lua-users home
lua-l archive

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


> My vote is for David Given's suggestion:
>
> match k
>   with 10, 11 then return 1 end
>   with 12 then return 2 end
>   with i if 13<=i and i<=16 then return 4 end
> end
>
> It's understood to involve just linear chains of elseifs ;)
>
> steve d.

It's too limited/fixed on k.  Suppose if k is greater than 12 another
condition needs to be checked, like z is not 0 and must be zero if k is 12
or less.  The above match syntax can't handle it, resulting long chains of
if-then-elseif, which I always find time-consuming to debug.

While in C and other languages that get compiled into machine code, I can
understand a fixed value that can be used for jump instructions, in an
interpreted language like Lua where speed is not the highest priority,
having more flexibility is reasonable.

I suspect it'd be easy to implement an approach that has a list of
statements to be evaluated and then the first one in the list that
evaluates "true" is the index to the block of code that gets executed.  It
can also superset the above match syntax inherently:

In Case of
     @(10 <= k <=11)
               then return 1
     @(k== 12)
               then return 2
     @(z == 0)
               then return 0
     @((k == i) && ( 13<= i <=16))
              then return 4
else
        then return 99

end

I use the "@" symbol to mark the beginning of the statement to be
evaluated (obviously) but it also eliminates the "double end" issue noted
by Fabien by demarcation of the start of the next conditional (possibly
handled by jumping to the end statement/epilog).  Also, Asko's version
didn't match David Given's version, as his version requires (as I read it)
for k to equal i as well as i be between 13 and 16, instead of k being
between 13 and 16.

tim

Gregory T. (tim) Kelly
Owner
Dialectronics.com


"Anything war can do, peace can do better."  -- Bishop Desmond Tutu