IMPORTANT
This is ultimately just a proof of concept. Ideally, you’d just use if statements or a lookup table rather than this module. Although I have attempted to make this reasonably performant enough to share as a comparison to other switchcase modules.
Key Features
- Passthrough support for different cases
- Lookup table generation for multi-key to value mapping.
Luau Switchcase
This a simple implementation of switchcases into Luau, built in such a way as to maximize reusability. It’s effectively just a wrapper for a lookup table so it’s fairly performant, especially because of my design approach.
Obtain the Module
The latest release of the module’s source can be viewed/obtained from its GitHub Repository.
You can also add it to your project via wally:
SwitchCase = "cens-r/switchcase@2.1.0"
Usage
Unlike typical switchcases in other programming languages, you can reuse the switchcase structure after its construction. This is the primary performance saver of the module as this is always the most expensive step in other implementations of this structure.
Example Code
-- Require the module:
local S = require(PATH.TO.MODULE)
-- For readability sake:
type Switch<T> = S.Switch<T> -- Typing support :D
local switch, case, default, proceed = S.switch, S.case, S.default, S.proceed
type output = {result: string}
local function GenerateOutput(message: string)
-- Dynamically generating functions for brevity
return function (out: output, arg: number)
out.result = message .. ` ({arg})`
end
end
-- Construct a switch statement:
local statement: Switch<output> = switch {
-- Fill in your cases:
case(0) { proceed }; -- `proceed` keyword used for pass-through
case(1, 2, 3) { GenerateOutput("Low") };
case(4, 5, 6) { GenerateOutput("Medium") };
case(7, 8, 9) { GenerateOutput("High") };
-- Optionally add a default case:
default { GenerateOutput("None") };
}
-- Use your statement:
for value = 0, 10 do
local out = statement:resolve(value)
print(out.result)
end
Example Output
Low (0)
Low (1)
Low (2)
Low (3)
Medium (4)
Medium (5)
Medium (6)
High (7)
High (8)
High (9)
None (10)
As you may have noticed from this code, you don’t need to include break
statements like you typically would for switchcases in other languages. This is because the module assumes this is the behavior you want in an attempt to reduce boilerplate. To get this pass-through behavior you’ll need to use the proceed
keyword.
There is an option to revert this to standard behavior through a flag within the module:
local S = require(PATH.TO.MODULE)
S.flags.ImplicitExitBehavior = false -- Its default state is `true`
When this flag is set to false
you’ll need to explicitly specify when pass-through should NOT happen using the keyword exit
.
Note: Both “keywords” (exit and proceed) are values found within the table returned from the module.
Benchmarking
I’ve only done benchmarking against my own previous versions of this module, but the results should be similar (more or less) for any other switchcase module that doesn’t follow a reusable design.