Really good module, but this takes more memory/performance than regular “if” statements.
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.
Update
now the module supports case stacking
Usage example
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)
}
An interesting module you got there.
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!
New style: Brackets
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
}
}
OH! In that case, this is super useful!!
Hi, I am making my own custom variant of lua. Can I have LuaSwitch pre-installed into it?
Yes, you can use LuaSwitch for all ur projects
I try this, but it says ServerScriptService.Script.MainModule:15: attempt to call a table value
Is this more performant than function dictionaries, if not then another system would be better.
This bug had already been fixed but I had forgotten to update the module in roblox. Update the module and try again
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
Will we ever be able to use booleans as cases instead of just strings?
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:
Switch(Subject)
.Case(Condition, function()
end)
.Case(Condition2, function()
end)
.Default(function()
end)
But I think this table-based implementation you’ve come up with, is super creative and clean.
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!
yes, just use the parenthesis when you are going to put the value for each case, example:
switch(value) {
case("Value") {
function()
print("Hello World!")
end
}
}
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.
Very useful module.
Beneficial & very easy to use. Great work!
Wow, it’s really cool!!! Finally a switch in Roblox!!!