lua-users home
lua-l archive

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


On 1/28/2018 4:26 PM, Sean Conner wrote:
It was thus said that the Great KHMan once stated:

How many other major languages that avoids having a switch
construct are there? Lua puts in a utf8 library but not a switch
construct?

   Not built in, but ...

	function switch(var,case)
	  if case[var] then
	    case[var]()
	  end
	end
	
	x = 4
	
	switch(x, {
	  one   = function() print "yada"  end,
	  two   = function() print "blah"  end,
	  three = function() print "yahoo" end,
	  [4]   = function() print "this is four" end,
	})

   You still have the overhead of a table lookup, but this is about as close
to a switch statement you can get in Lua without changing the language.

There's a lot of legacy wisdom here:

http://lua-users.org/wiki/SwitchStatement

I'd like a native switch. :-) Please, pretty pretty please :-)

--
Cheers,
Kein-Hong Man (esq.)
Selangor, Malaysia