This is obviously a perenial topic, since there's a wiki page
dedicated to
it:
http://lua-users.org/wiki/SwitchStatement
I remember once hearing Tony Hoare give an awards lecture in which he
told us the three things he was most proud of:
1. Quicksort
2. Inventing the case statement
3. Starting and ending his career in industry
I am a huge fan of case statements and miss them greatly when
programming in Lua. I actually implemented case statements in my
lua2.5+nw extension to Lua, used in Noweb 3. There are some fairly
severe constraints owing to Lua's one-pass compilation model.
A few comments:
* While I love Haskell/OCaml matching with pattern guards, I don't
see that as a good model for Lua, because Lua has no notion of
constant or constructor
* Dispatching on a table of functions can be very effective in many
situations, but given Lua's heavyweight syntax for functions, it
is not as pleasant as a case statement. I also shudder at the
cost of allocating all the closures every trip through---but it is
fine at toplevel if done only once.
* Of existing languages with case statements, the model I know of
that most closely fits Lua is the Icon programming language.
There as in Lua there are no special constants and the case
expression is simply another way of writing nested if's.
Bottom line: I feel that the case statement is a vital tool for a
programmer who strives to write crystal-clear code. For Lua I think a
simple linear-time implementation is perfectly adequate (and I have
the experimental evidence to prove it). This feature is perennially
on my wish list for the next version of Lua. But I have failed to
convince Roberto and Luiz, even when I tried to embarrass them in
public earlier this summer :-)
I'm not sure of the current state of token filters; I will be
interested to see if anyone succeeds along those lines. (Although I
must confess that token filters remind me unpleasantly of the failed
experiments with extensible syntax in the 1960s. I would much rather
have a single case syntax that is blessed by the Lua team.)
Norman