✓ Accepted answer
I have looked into a bit about Switch statements and realized that Lua does not support such statements.
Note: I use switch statements regularly in other programming languages, and the opinion below is after careful consideration for the pros and cons of both the switch statement and whether it belongs in the context of Lua.
When I write code in Lua, I don't write code as if I want to write in C, Python, Java, JavaScript, Ruby, etc. You should avoid implementing features that a language lacks over another language. Most of the time, your choice is unjustified and silly.
Anyhow, switch statements are probably most notable for the following characteristics:
only compare once
only compares against constant-expressions
Some nearly irrelevant characteristics are:
are prettier (to some people)
are fewer characters
are faster (sometimes, but this is often due to compiler optimizations)
In all honesty...
1 & 2) to replicate switch comparison behavior you're probably going to turn to using a dictionary-style table (though this doesn't truly replicate switch behavior either). This is overcomplicating your code just to get some pretty styling rather than actually replicating switch behavior. If you are not actually replicating switch behavior, there is no reason to try doing it. You'll only make your code more complicated for both yourself, anyone who reads your code, and the Lua interpreter.
pretty code is not always good code.
this is a ridiculous reason to want to implement switch behavior.
your implementation is probably slower anyway.
Please do not use a switch statement imitation in Lua. You can do it for curiosity's sake, but it is really useless in this context and really not worth your time.
1 comment
Thanks a lot, I use them very often myself. Although, I never really had a reason to do it on Roblox. I just figured it would be something worth learning. Thanks for the answer.