Skip to content

Instantly share code, notes, and snippets.

@MineRobber9000
Created July 22, 2023 04:59
Show Gist options
  • Save MineRobber9000/096b60c019ae843f15a04c4babf63347 to your computer and use it in GitHub Desktop.
Save MineRobber9000/096b60c019ae843f15a04c4babf63347 to your computer and use it in GitHub Desktop.
syntax sugary switch case in lua
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