LuaSwitch | Switch for roblox

About the project

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

Repository

Module

https://www.roblox.com/library/9612005731/LuaSwitch

Usage Example

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)
}
24 Likes

Huh? I don’t understand what a switch is. This seems very interesting tho.

3 Likes

Really good module, but this takes more memory/performance than regular “if” statements.

6 Likes

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.

2 Likes

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)
}
4 Likes

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?

2 Likes

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!

1 Like

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
    }
}
1 Like

OH! In that case, this is super useful!!

1 Like

Hi, I am making my own custom variant of lua. Can I have LuaSwitch pre-installed into it?

1 Like

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

1 Like

Is this more performant than function dictionaries, if not then another system would be better.

1 Like

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?

1 Like

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.

1 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!

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.

1 Like