lua-users home
lua-l archive

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


Sean Conner <sean@conman.org> wrote:

> Talking about switches.  One thing I often wish I could do is the
> following [1]:
> 
> 	switch(c)
> 	{
> 	  case < 0 : /* handle case */
> 	       break;
> 	  case == 0 : /* handle this case */
> 	       break;
> 	  case > 0 : /* and this case */
> 	       break;
> 	}

Well the == case could just leave off the == bit, but the < and > versions
are interesting to contemplate.

In an earlier discussion with Soni we were discussing non-constant case
values, for which it would probably be simpler to devolve the switch/case
construct to an if/else system, but with special OP_JMP opcodes emitted
in the case that fallthrough was required.

These type of comparison case values could be added to such a system, so
I will definitely keep this idea in mind!


> I do recall reading about a proposed language back in the mid-80s that
> unified if-then with switch (with the added bonus of removing the keywords!)
> which is where I got the above.  The actual proposal was:
> 
> 	(?var
> 	  = value1 : statements ;
> 	  = value2 : statements ;
> 	  = @      : statements ; /* default value */
> 	?)

This looks like some strange merging of if/else, switch/case and the
ternary operator... which is something else I want to implement.


> where '_' is a "don't care what the value is."  A better example would be
> this (note:  the code was written for a wide window):

In my current mega-patch I have added the ability to use 'nil' to
indicate return values that should be discarded. It works quite well!


>  I'm going through my C code, looking at the various switch statments, and
> a few things jump out to me.  One, most of them are to handle a sequence of
> values (usually an enum) that in most cases, a jump table can easily handle. 
> Two, state machines, whehter in parsing text or handling network protocols. 
> And occasionally to avoid the ugliness of an if/elseif chain.


Yes, enums are a great use of switch/case, and accordingly I have created
the ability to add consts and enumerations as well... which makes the code
look so much nicer vs magic numbers, or having to use global variables to
hold what are essentially constant values.

I also realise now that I could make a slightly more efficient switch/case
construct in the case where the case values are all enums of the same type!


>  As far as fallthrough though,, that is *so* rare in my code as it's not a
> consideration for me.  I *have* used fallthrough, but generally it's only
> when parsing the command line:


I noticed in my research that not all languages even support fallthrough in
their switch constructs, however, it wasn't hard to implement so I figured
why not offer it as an option?

The source code of Lua itself does use fallthrough in a number of places
as well to good use. It may not necessarily be common but I think providing
the facility for fallthrough is worth having.


>  That said, I was reminded of this article:
> 	http://prog21.dadgum.com/166.html [1]
> [1]	Check the archives for there is a lot of good material here.  Lots
> 	of food for thought.

Thank you for that link, I have bookmarked it for later reading! :)

~Paige