Skip to content
This repository has been archived by the owner on Oct 9, 2024. It is now read-only.

Switch.luau is a switch implementation in pure luau including the main switch function and also a case and default function.

License

Notifications You must be signed in to change notification settings

Sparkypen7/switch-luau

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Switch.luau

Switch.luau is a switch implementation in pure luau.

Why use a switch statement over an if-statement?

Switch statements, popular in many programming languages, offer a cleaner and more readable solution to deeply nested if-else statements. Switch statements also handle fall-through cases and defaults, reducing the chances of logic errors.

With switch statements:

local Switch = require(...) -- Change ... to the path of the ModuleScript

local numberVariable = 10 -- this here is the variable we will be checking

Switch.new(numberVariable)
    :Case(468, "==", function()
        print("numberVariable is equal to 468")
    end)
    :Case(20, ">", function()
        print("numberVariable is greater than 20")
    end)
    :Default(function()
        print("numberVariable doesn't match the cases")
    end)

Output: numberVariable doesn't match the cases

Without switch statements

local numberVariable = 10 -- this is the variable we will be checking

if numberVariable == 468 then
    print("numberVariable is equal to 468")
elseif numberVariable == 11 then
    print("numberVariable is greater than 20")
else
    print("numberVariable doesn't match the cases")
end

Output: numberVariable doesn't match the cases

Conclusion

Ultimately, switch statements are more readable. Especially if you have a lot of nested if-statements in your code. Switch statements allow us to space out our cases as much as we want. They also make it easier to remove a condition.

Switch statements are also more diverse. You can switch it up and define your default function before your cases; or, you can define a default in between two cases.

Installation

Via Github Releases

You can install the switch module via github releases by downloading the .lua file under the latest release.

Via Wally

You can also use wally to install the switch module by inserting: switch = "veternitzz/[email protected]" into your wally config file.

Note: No wally releases before 1.0.4 are stable, as I had a little bit of an issue publishing this module.

Api Reference

Constructors

Switch.new(): metatable

Parameters

Parameter Name Type Description
variable any The variable to check

Methods (switch.new)

Switch:debug(): nil

Description Enables or disabled debug logging. This function was only designed for debugging and will print when a case or default is registered.

Parameters

Parameter Name Type Description
enabled boolean Whether to enable or disable debug logging

Switch:Case(): Switch

Description Registers a case if the value is met and calls the parameter func. There is no limit to how many cases you can have.

Parameters

Parameter Name Type Description
value any The value to compare the variable to
operation "~=", "==", ">", ">=", "<", "<=" The operation to perform
func any (must be a function) The function to call if the value is met

Usage Example

local a = 47

local conditional = switch.new(a)

-- Because the operation is ~= (not equal to), the output will be "Hello world"
conditional:Case(46, "~=", function()
    print("Hello world")
end)

Switch:Default(): nil

Description The default case. This acts as an else statement and will call the func parameter if no cases are met. There may be only one registered default case per constructor.

Parameters

Parameter Name Type Description
func any (must be a function) The function to call if the value is met

Usage Example

local a = 57

-- The output will be: "Default"

Switch.new(a)
    :Case(56, "==", function()
        print("56")
    end)
    :Default(function()
        print("Default")
    end)

About

Switch.luau is a switch implementation in pure luau including the main switch function and also a case and default function.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Luau 100.0%