Created
August 3, 2015 11:04
-
-
Save picsoung/8e04fac92f883b3c9d77 to your computer and use it in GitHub Desktop.
a switch in Lua
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- fct to simulate a switch | |
function switch(t) | |
t.case = function (self,x) | |
local f=self[x] or self.default | |
if f then | |
if type(f)=="function" then | |
f(x,self) | |
else | |
error("case "..tostring(x).." not a function") | |
end | |
end | |
end | |
return t | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
usage
found here: http://lua-users.org/wiki/SwitchStatement