Hello!
I have created a simple module for C’s Switch … Case in lua.
If you don’t know what switch case is, it is basically a faster way to write if statements.
This is a very simple module but it can be useful.
An example of using this is:
switch, case, default = nil, nil, nil -- silences errors
require(game.ReplicatedStorage.SwitchCase).init()
local value = 1
switch (value) {
case (1) {
function()
print("The value is 1")
end
},
case (10) {
function(val) -- the first arg is the value
print("The value is "..val)
end
},
default {
function(val)
print("Unknown Value("..val..")")
end
}
}
Here is the github with the code and more details: https://github.com/hello123991/LuaSwitchCase
(Sorry the code is messy lol)
Thanks! Let me know any suggestions or bugs. I might make it a bit easier to write the cases because I tried to make it look as much like C++ as possible, but it made it slower to write.
12 Likes
Why would you have an entire module for something that can be done in vanilla Lua?
Also, correct me if I’m wrong but from what I recall switch-case acts similarly to an if statement, meaning it has O(n) time complexity, meanwhile doing a table-function approach has a O(1) time complexity, since we’re accessing a fixed address in the array.
Just checked the source code and yes, you’re better off with the vanilla Lua approach. The amount of resources and runtime for your code to run completely exceeds even a normal if-tree.
You had good intentions but I don’t think this is fit at all.
local value = 1
local switch = {
[1] = function()
print("The value is 1")
end,
[10] = function()
print("The value is 10")
end,
["default"] = function()
print("Unknown value")
end
}
if switch[value] then
switch[value]()
else
switch["default"]()
end
4 Likes
Would it be better to have a syntax like this?
local switch = require(path.to.Switch)
local value = 1
switch (value) {
[1] = function()
print("The value is 1")
end,
[10] = function(val)
print("The value is " .. val)
end,
default = function(val)
print("Unknown value (" .. val .. ")")
end,
}
It would make the implementation for switch
very simple and possibly more performant:
function switch(value)
return function(cases)
local case = cases[value] or cases.default
if case then
return case(value)
else
error(string.format("Unhandled case (%s)", value), 2)
end
end
end
return switch
This also allows you to return a value from a switch
statement:
local enemyType = "vampire"
local enemy = switch (enemyType) {
["zombie"] = function()
return Zombie.new(...)
end,
["vampire"] = function()
return Vampire.new(...)
end,
default = function()
error(string.format("Unknown enemy %q", enemyType))
end
}
There is one small issue with this. If you try to use switch
on a string and one of those possibilities is "default"
, it will always run the default
case because both represent the same key.
A cleaner version of this code without proper error handling, just because it looks nice
function switch(value)
return function(cases)
return (cases[value] or cases.default)(value)
end
end
return switch
5 Likes
Yeah, that is a much cleaner approach to an inexisting problem. I like the sugar syntax though, might use your approach in my projects.
3 Likes