Switch. A SwitchCase Simulation

the greatest things get invented during boredom


Switch - Switch Case Statement Simulation in Lua

A small module for creating a switch case simulation with Builder syntax.
This attempts to mirror the behaviour of switch case statements in Visual Basic.

Module Link: Switch Module - Roblox

Documentation

Switch Module

local Switch = require(Switch)

Returns a function which can be used for constructing switch case objects (see example)


Switch Case Object

Metamethods
__call (when it is called like a function) - Runs the switch case statement
Example:

SwitchCase(n) --n is the variable to be checked against the condition

Properties:
dictionary _callbacks - A storage table used for holding the callbacks
function _default - The default function for undefined conditions

Methods:
SwitchCaseObject case(value, callback) - binds a callback to specified value.

WARNING: This function has only been tested on strings and numbers, it may behave weirdly with other datatypes

SwitchCaseObject default(callback) - binds the default function to a callback

Example

Below is an example for Switch

local Switch = require(script.Switch) --Load the Switch module

local newConditional = Switch() --Create a new SwitchCase using builder syntax
  :case("Player1", function() --Setup a case value callback
    print("Player 1 is outstanding")
  end)

  :case("Player2", function()
    print("Player 2 is amazing")
  end)

  :default(function() --allows you to manage undefined conditions. SwitchCase will error if this is not declared and you use an invalid value
    print("not declared")
  end)

local Player1Name = "Player1"
local Player2Name = "Player2"
local unwantedValue = false

newConditional(Player1Name) --> Player 1 is outstanding
newConditional(Player2Name) --> Player 2 is amazing
newConditional(unwantedValue) --> not declared
Issues

Make sure to leave issues below if you find any.

51 Likes

Nice module, I’ve created one like this before and it’s super helpful.

Would you be able to use a pastebin paste to show the source to those who cannot view it currently?

3 Likes

Source Code is now available here

1 Like

Switch cases usually has a break statement, how is this use case supported?

Weird enough I just learnt what these were today and all of a sudden this pops up. Great work I’ll be sure to use it in the future.

Since this uses callbacks instead of it being a statement, break statements dont need to be used, I already knew most other languages with this implementation use break part of it’s statement.

It’s not worth implementing something like this if it’s unnecessary and may affect performance.

1 Like

A really simple, but useful module. Thank you.

2 Likes

I guess you can pretend that end) is break

Edit: annnd I reply to the wrong person again. Sorry about that!

You could pass a controller object through that would indicate to the switch object to skip the remaining cases when invoked. Ex:

--Values passed through 'resolve' would be returned instead of what the actual function may return (similar to how promises work)
Switch()
    :case('Player1', function(resolve)
        --do stuff
        resolve(...)
    end)

I only suggest this up because multi-case operations would be a nice addition that would reduce boilerplate code.

1 Like

How is this better than If and ElseIf Statements (Just a Question)

1 Like

it’s just syntax sugar mostly. some people might prefer that

https://www.google.com/search?q=switch+statement+vs+if+else&rlz=1C1CHBF_enUS902US902&oq=switch+statement+vs+&aqs=chrome.0.0j69i57j0l6.3266j0j7&sourceid=chrome&ie=UTF-8

tl;dr easier to test many expressions

2 Likes

Usually switch statements are better in performance for large amounts of conditionals but its hard to say for this LUA switch statement

1 Like

Yeah, Lua(u) has no switch, so you don’t get the compile-time benefits in Lua(u) that you would get in a language that actually has this.

Not sure if I did something wrong, but creating a new switch case gives you an error making the module unusable:

attempt to call a Instance value.

Can you send your code so that I can debug the issue, just sending the error wont tell me what the issue is.

1 Like

Oh my bad. This is the script:

local a = "2"

local switch = game:GetService("ServerStorage").Extensions.Switch_Case_Simulation

local newConditional = switch() --This errors

:case("2", function()
	print("Correct value!")
end)

:case("1",function()
	
end)

newConditional(a)

You did not require the module. You’ve created a reference to the Roblox object, not the table returned by it

3 Likes

Oh that’s a stupid mistake, I always forget to add ‘require’ on modules :sweat_smile:

Sorry for bumping but lets say a thousand of switch cases would be used for whatever reason would it still perform really well?