As a Roblox developer, it is currently too hard to manage spaghetti code with many if/else statements, and there are solutions within the C language and C++ languages for this very thing. I would like to request Switch statements.
If Roblox is able to address this issue, it would improve my development experience because I could cut out If/Else spaghetti, instead having one big switch statement that can check a bunch of stuff.
New language features come with a pretty big price, we need use cases in order to help estimate the benefits of that cost. Can you provide use cases where an if/else is substantially less readable than switch/case?
For example, something like this:
if color == "red" then
-- do stuff
elseif color == "blue" then
-- do stuff
elseif color == "green" then
-- do stuff
end
âŚis not really much less readable than:
switch (color) {
case "red":
// do stuff
case "green":
// do stuff
case "blue":
// do stuff
}
âŚand also adds an extra level of indentation and loss of control in being able to add extra conditionals.
I also find that it would be helpful to have something that doesnât break at the first true statement. Switch cases will check everything provided, if/else does not. A practical use case would be to check something like an achievement system. I have 5 types of data I need to check for and if/else will only check for 1. Iâd like to be able to avoid
if datatype then
end if otherDataype then
end -- repeat 5 times
If there is a better way than switch cases (or if I am mistaken about some of the logic here), let me know, but I feel like switch cases are far more readable and more practical than the above.
Small edit, I made it end if instead of elseif because of the âbreak at first true conditionâ thing above
You shouldnât worry too much about thatâwe already contextually parse specific identifiers as keywordsâcontinue being the notable example, where you can name a variable continue and use it everywhere you couldâve used a normal variable, but we understand that it can be its own statement as well.
Just because other languages have a feature doesnât mean every new language needs to be a collection of them + more.
Languages are tools to accomplish a goal - and if/else definitely allows you to do more than anything you need. Bloated languages can be less approachable for newer / younger users, and also lead into pitfalls when you donât remember minor edge cases (for example, Lua arrays starting at 1)
I literally just learnt what a switch statement was in computer science today and im surprised i have never heard of it before. This would be pretty useful if it was implemented
I would absolutely love for this to be a feature. The difference in readability while not majorly different, I do think that âcaseâ is a lot cleaner. Especially when your game has a huge ton of if/elseif everywhere and it gets kind of cluttered.
There may be need for extra indentation, but that is a cost Iâm willing to pay. I also think itâd just be nice to have different ways to handle logic cases. Iâd personally use this if it became a feature.
Can you provide a code snippet example of what you mean? What specific set of if statements did you use that led you to want this? Iâd like to review your code to understand what youâve written. This way, I can explore potential alternatives to avoid the need for a large number of if statements.
Well this seems kind of vague, not a lot to go off of, but here
local achievement_unlock_types = {achievement_unlock_type_1, achievement_unlock_type_2, achievement_unlock_type_3}
for _, unlock_type in pairs(achievement_unlock_types) do
if unlock_type then
end
end
I would be glad to have this! This would probably allow more optimizations to the code and not breaking the first statement while looking for more applications sounds too good to be true. The thing is that itâs limited to only one conditions.
For the colon syntax, I wouldnât suggest to have it on Lua.
When handling alot of possible states and when some states have the same result, rather than doing color == "blue" or color == "green" a simple:
case "blue" do
case "green" do
-- code
can be applied.
Also I know this is unrelated to the whole switch-statement conversation but I think gotos will also be really cool and provide a HUGE performance increase for many programs that require moving from part to part of the program without using functions. Especially if it is implemented in an optimized fashion.
For context, Apakovtac and zeux are both engineers on the Luau team, so this is going to be a pretty definitive no, at least in the specific form of switch statements.
What is more likely is an equivalent to match expressions. Here is some prior art:
Theyâre like more powerful switch statements, in that you could do something like this (using a pretend Rust/Luau syntax as an example)
let x = match part.position {
Vector2(1, 1) => "top right",
Vector2(x, 1) => `{x}, right`,
Vector2(1, y) => `top, {y}`,
Vector2(x, y) => `{x}, {y}`,
}
But this is not a promise and will not happen soon.
Anything with some sort of fallthrough is also very unlikely to meâfallthrough is something modern languages donât really implement to my knowledge, and even modern C++ codebases will actively lint against them, requiring some sort of explicit marker.
In the 13 years Iâve been writing Lua code on Roblox I have never once run into a scenario where I wanted to use a switch statement. Luaâs simplicity & limited syntax is what makes it approachable.
The only reason for a switch statement that I can think of is to shorten some if-else code by a few lines, but in cases where you have a long string of if-statements you want to replace with a switch-statement, there are probably way better solutions. For example when programming keyboard movement, instead of using this:
function processKey(key)
if key == Enum.KeyCode.W then
elseif key == Enum.KeyCode.A then
elseif key == Enum.KeyCode.S then
elseif key == Enum.KeyCode.D then
end
end
You could use a dictionary instead, such as:
local inputFunctions = {
[Enum.KeyCode.W] = func1;
[Enum.KeyCode.A] = func2;
[Enum.KeyCode.S] = func3;
[Enum.KeyCode.D] = func4;
}
function processKey(key)
inputFunctions[key]()
end