local default = newproxy()
local function switch(case)
return function(callbacks) --[[ I can't think of any way to implement passthrough without funky syntax ]]
return (callbacks[case] or callbacks[default] or error(`No default case or handler for {case}`)()
end
end
switch(10){ -- This is legal in Lua(u) and considered calling with a table
[10] = function()
print("Is 10")
end
[default] = function()
print("Cannot be handled")
end
}
return function (case : string | number)
return function(callbacks : {[any] : () -> any})
return (
callbacks[case]
or
function()
print(case, 'is not a case of switch')
end
)()
end
end
local switch = require(...)
switch (10) {
[10] = function()
print('chat its 10')
end;
[15] = function()
print('chat what the filip its not 10 its 15')
end
}
One cool thing about switch statements is that they have much better perfomance compared to if else statements.
Are switch statements in lua required? Maybe. Would it be cool to have them? Yes.
Switch statements are fast in other languages because they can be optimized into a simple look-up table where each value tells the system to ājumpā to a specific section of the code. I feel like Luau could have these optimizations for if statements you could replace with a switch statement in other languages, though, so I donāt really think optimization is an advantage to adding switch statements.
But yeah I think it would be cool to use them instead of long else-if chains, but Iād be content with a custom implementation like the ones shared in this thread.
I would also like to add that switch statements have the potential to be faster than if statements, as they can be optimized, taking into account that theyāre only going to check for equality
If we can make these optimizations with switch, we can make them with normal if statements as well. Thereās no reason we canāt analyze something like this:
if color == "red" then
-- red
elseif color == "green" then
-- green
ā¦the same way we would an ordinary switch when it comes to optimization. I cannot speak for if we do any kind of jump table optimizations or whatnot today, thoughāI donāt know that part.
Yep, I saw that right after I made the post. Whatās the reasoning behind not wanting to implement it? I understand other modern languages donāt use it but I donāt see why Luau has to copy others (itās already pretty unique as is).
From what I have seen, some people here donāt really know that a switch/case statement checks one variable only. Similar to other solutions here, this is how I deal with it:
local function f1(params)
-- Do something
end
local function f2(params)
-- Do something
end
local function f3(params)
-- Do something
end
local function f4(params)
-- Do something
end
local functionCallTable = {
[value 1] = f1;
[value 2] = f2;
[value 3] = f3;
[value 4] = f4;
}
if functionCallTable[check value] ~= nil then
functionCallTable[check value](params)
else
-- Do something else
end
As you can see, there are ways around a missing switch/case statement. I personally would love to have a switch/case statement in LUA, but it must be compatible with existing code. Someone mentioned that if switch and case became reserved words, a lot of code would break, and I donāt think itās worth it. With that being said however, thereās nothing that says that switch/case can be called something else.
Another thing that comes to mind is the LUA JIT Compiler: Have it look for the above patterns and use a switch/case internally. That will require complex alterations to the lexicographic analyzer and the parser. However, an alternative to this is to modify the optimizer. When the AST (Abstract Source Tree) is analyzed, the optimizer can look for these patterns and optimize it accordingly for the code generator.
Or add a directive that can be placed in the source code to tell the compiler that this construct is a switch/case pattern.
There are always alternatives.
One thing that I want to point out about the if-then-elseif-else statement chain is the benefit of being able to check completely different cases with completely different variables within the chain.
Matching naturally cover a wider use case than switch statement. Things like table patterns, as an example. It makes more sense to only add one of them to the language, so it seems reasonable to add pattern matching.
if/else statements are vroom in Luau, idk what optimisation black magic they use for that.
I have a module with over 30 if-else branches and its very readable. idk why Luau needs another control flow option when the existing one works perfectly.
Just add a line of whitespace between the end of a branch and a new condition