this morning I was bored and a friend was wondering why there is no switch in roblox, I explained to him that there are better ways to solve the problem than the switch try to solve, but I still wondered how a switch could be made, and here is the module
local switch, case, default = require(LuaSwitch).getFunctions()
local myUnknownPet = if math.random(1, 2) == 1 then "dog" else "cat"
switch (myUnknownPet) {
case "dog" (function(stop)
warn("My pet is a dog!")
stop() -- skip default and stop to check the other cases, but doesn't stop the execution of the function
end),
case "cat" (function(stop)
warn("My pet is a cat!")
stop()
end),
default(function()
print("I don't know what my pet is :(")
end)
}
A switch statement, which is what LuaSwitch is trying to implement for Lua, is a fancier version of if-elseif-else statements you are familiar with in Lua. It makes certain things like doing something based on what the input is equal to a lot easier and even more readable.
Switch statements are found in plenty of programming language, like Java and C++.
They can be useful and a better alternative to the normal if-else statements depending on what you trying to do. However, they do suffer from the same problems as if-else statements, and also have their unique set of issues that don’t exist for if-else statements.
local switch, case, default = require(LuaSwitch).getFunctions()
local value = 0
switch("B") {
case "A",
case "B",
case "C" (function()
value = 2
end),
default (function()
value -= 1
end)
}
Is there any benefit to using a switch statement (versus an if-then statement) other than it looks more readable? Maybe an example of a situation where a switch statement would be favored more than an if-then statement?
When switching from Java to Luau, switches were one of my biggest desires! Really cool you made that possible.
Although I don’t think I’ll use it personally, I feel the additional work it takes to import the module into each script whenever I’d rather use a Switch over an If-Statement is too much work. It’d be great if Roblox implemented this directly into Luau though!
local switch, case, default = require(LuaSwitch).getFunctions()
local value = "B"
switch(value) {
case "A" {
function()
print("Case A")
end
},
case "B" {
function()
print("Case B")
end
},
case "C" {
function()
print("Case C")
end
},
default {
function()
print("Executing default")
end
}
}
and no, possibly it is not as efficient as dictionaries, but as I said this is a project I have done for fun, I do not intend to overcome the performance of dictionaries, however the module still has a performance similar to that of a if
This is an excellently clever use of Lua’s syntax sugar. It’s a shame that bracketless calls can only be used on tables and strings; it would be nice to see the ‘case’ function be able to take any value, without the use of additional parenthesis.
Intuitively, I might have leaned toward chaining function calls. Something like:
thanks, this is just a project I did to distract me a bit from the development of my 2d game engine, as it was very saturated, but maybe when I finish it I will also add the possibility of using the switch as you suggested!
Looks like you got to this before me, I was about to post a resource of my switch-case project but you got there first. Kudos! Anyways, I will be back on my way of fully making a parity of JS/TS in Luau.