lua-users home
lua-l archive

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



How about making a "competition" or a quest for the perfect switch (if there is one) right here on the mailing list?

In other words, instead of tossing the idea around and about, let's start narrowing it to what we'd actually want to have, and make an implementation out of it.

The frame conditions would be:

- no patching necessary
- syntax modifications (token filtering) is usable

I would do the proposed switch/case implementation for luaSuper, once it comes out. What I need is the syntax structure and corresponding Lua code.

btw, about " failed experiments with extensible syntax in the 1960s", what exactly did you mean? How did they fail and would the IT environment still be similar, or changed enough?

-asko


Norman Ramsey kirjoitti 9.11.2007 kello 3:21:

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