lua_switch, a shorthand for creating switch statements in lua
Posted: Mon Jul 04, 2022 1:28 am
Someone in the discord was surprised that there was no internal method to create a switch statement in Lua.
So being half awake, I thought to myself: "I can fix that."
Then this thing was made. If it's useful to you, I'm glad. Have fun with it.
https://github.com/jordan4ibanez/lua_switch
usage example: (the tutorial from the repo)
this will print out:
So being half awake, I thought to myself: "I can fix that."
Then this thing was made. If it's useful to you, I'm glad. Have fun with it.
https://github.com/jordan4ibanez/lua_switch
usage example: (the tutorial from the repo)
Code: Select all
require("switch")
-- a basic switch statement definition
local test_switch = switch:new({
test = function()
print("test worked")
end,
[5] = function()
print("math is cool")
end
})
-- we utilize it
test_switch:match("test")
test_switch:match(5)
-- another one defined
local name_switch = switch:new({
fred = function()
print("wow fred is awesome!")
end,
jonny = function()
print("yeah that's jonny")
end
})
print("\n")
-- we utilize it like so
local name_repo = {
"frank", "fred", "john", "jonny", "zoop"
}
for _,name in ipairs(name_repo) do
name_switch:match(name)
end
print("\n")
-- we can also inline this whole thing
local my_functional_switch = switch:new({
test = function()
print("YEAH THIS WORKS BOI")
end,
[100] = function()
print("that's a pretty big number")
end,
[false] = function()
print("yeah that's wrong")
end
}):match(false)
Code: Select all
test worked
math is cool
wow fred is awesome!
yeah that's jonny
yeah that's wrong