lua-users home
lua-l archive

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


I'd like to see Lua's tooling ecosystem improved. As 2017 showed, the tooling a language has on offer is just as important as the language itself.


On Sun, 28 Jan 2018, 9:26 am Sean Conner, <sean@conman.org> 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.

  -spc