Created
July 22, 2023 04:59
-
-
Save MineRobber9000/096b60c019ae843f15a04c4babf63347 to your computer and use it in GitHub Desktop.
syntax sugary switch case in lua
This file contains hidden or 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
local _nil = setmetatable({},{__newindex=function() end}) -- sentinel | |
local function switch(val) | |
if val==nil then val=_nil end | |
return function(cases) | |
return (cases[val] or cases.default or function() end)() | |
end | |
end | |
return setmetatable({["_nil"]=_nil,switch=switch},{__call=function(t,...) return switch(...) end}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment