A builder-style switch / pattern-matching library for Lua. Thin wrapper around matchigo-lua with events, middleware, opt-in memoize, and a named registry.
- Installation — Lua, FiveM, Roblox, LÖVE2D
- Getting started — Your first switch in under 2 minutes
- Migration v1 → v2 — If you used the previous version
- Module —
EasySwitch.new(), registry,P,FALLTHROUGH,Map/Set/BigInt - Switch instance — Every method on the switch (
:when,:execute,:memoize, ...) - Events — All seven events with their payloads
- Patterns — Curated
P.*reference
- Pattern matching — Bare tables,
P.shape, unions, intersections - DSL strings — Detection rules, scope tables, common pitfalls
- Memoize — When to opt in, verify mode, auto-invalidation
- Middleware — Chain semantics, error isolation
- Fallthrough —
FALLTHROUGHsentinel + propagation - Named registry — Anonymous vs named instances
- HTTP router — Method + path dispatch
- Game state machine — LÖVE2D-style states + transitions
- FiveM job dispatch — Realistic FiveM resource
local EasySwitch = require("easyswitch")
local P = EasySwitch.P
local sw = EasySwitch.new()
:when("GET", function() return "list" end) -- literal
:when({"POST", "PUT"}, function(m) return "write:" .. m end) -- array of literals
:when(P.string, function(v) return "str:" .. v end) -- pattern
:when(P.number, function(n) return n > 0 end,
function() return "positive number" end) -- guard
:when("'DELETE' | 'PATCH'", function(m) return "modify" end) -- DSL string
:default(function() return "fallback" end)
:on("noMatch", function(v) print("unhandled:", v) end)
:memoize() -- opt-in cache
print(sw:execute("GET"))