lua-users home
lua-l archive

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


It was thus said that the Great KHMan once stated:
> 
> I think certain thinking processes when coding translates well to 
> a switch/case kind of construct, but it doesn't happen often 
> (well, depends). When it happens, the coder stops and asks, why 
> can't I use switch/case here?

  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;
	}

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 */
	?)

A simple 'if' statement would be:

	(?var = x : statement ?) 
	?var = x : statement ;

And this variation:

	(?
	  M  = 'C'; 'S' ; 'Q';
	 ^n := 1;    2;    3;
	?)

which assigns n depending upon what is in M.  There's also Erlang's switch,
whch uses patterns [2] to decide what to do:

	case Position of
		{ X , Y , Dir }         -> do_that();
		{ X , Y , Dir , _ , _ } -> do_thing();
		{ X , Y , _ , _ , _ }   -> do_other();
		{ X , Y }               -> do_this();
	end

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):

scorestrike( [ {First,Second}                | _ ]) when First < 10, Second < 10 -> First + Second;
scorestrike( [ {10   , _    } , {Third , _ } | _ ]) when Third < 10              -> 10 + Third;
scorestrike( [ {10   , _    } , {10    , _ } | _ ])                              -> 20.

  The Erlang compiler will figure out how best to represent that (internally
treated like a series of if/else or a switch or whatever).

  -spc (There are a lot of ways to go with this)

[1]	Related to that:

		0 <= x < 15

	but I don't know of any language that supports that type of check.

[2]	Not Lua patterns.