lua-users home
lua-l archive

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


On Apr 14, Mark@nlcc.demon.co.uk (Mark Ian Barlow) wrote:
> Yielding a translated and expanded example:
> 
>   with x choose
>     "foo" does
>       y = 1  z = "A"  end
>     default
>       y = 99  z = ""  end
>     "bar" does
>       y = 2  z = "B"  end
>   end

I agree 100%. This seems to fit better into the kind of syntax lua
promotes. 

> I'm not too sure about "with x choose", however, and I'd welcome a better
> suggestion...

My first thought was that "caseof x" (or "caseof x :" to have a
terminating characer) would make sense. However, I like "with x
choose" it seems more readable to me (i.e. for non-programmer types
especially)

> As implemented Norman's case construct is not a lot more efficient than
> a long "if elseif... else end", although the introduction of DUP helps.
> 
> In my original post in response to LHF's "wishlist" call, asking for a case
> construct I envisaged it being used when you had, say, >10 choices to cover
> and being implemented by using the value of the control expression to index a
> hidden table of anonymous functions, each of which would take no parameters
> and return nothing.

I had the same thought, except that if it really was implemented as
anonymous functions, local variable scope would be confused would it
not? (Just starting to use Lua, so correct me if this is wrong)

> This would give a real efficiency gain when there are a lot of choices by
> leveraging Lua's built-in hashing abilities (Is this not what case statements
> are _for_?). The above syntax would, I think, be cleaner if this method were
> used, and the "default" case can go anywhere; it is just another table entry
> like the rest, but with a special unique index...

agreed 100%. 

-- 
David Jeske (N9LCA)   +   jeske@igcom.net   +   http://www.igcom.net/~jeske/