This library is in flux. APIs may change without notice. Generated from source with koruc 0.1.7 on 11/4/2026.
Switch
import std/switchKoru Standard Library: switch — compile-time exact-value dispatch as control
switch.kz · 1 tors · ~[comptime]
Koru Standard Library: switch — compile-time exact-value dispatch as control · 15 more lines
Koru Standard Library: switch — compile-time exact-value dispatch as control
flow. Sibling of std/regex:match: same branch surface, but dispatches on a
single scalar value with a native Zig switch instead of a regex DFA.
std/switch:char(ch)
| `a` c |> got-a(c)
| `b` c |> got-b(c)
| no-match |> other()
The branch head is the opaque backtick string. The transform parses it as:
- single char: `a`
- inclusive range: `a-z`
- set of chars: `aeiou`
and emits a Zig switch on the dispatched value. First matching arm wins;
`no-match` is the optional fallback.
// The tor declares the USER surface: any raw-name branch carries the matched
// char (bind or discard with `_`), plus an optional void `no-match` fallback.
// The shape checker enforces this contract; the transform owns pattern parsing
// and code emission.
~[comptime|transform] pub tor char {
expr: Expression,
reporter: std/compiler:*ErrorReporter
}
| * *
| ?no-match~part emit koru_std/switch.emit.kz
Dispatch emission for std/switch:char. The transform splices host text into the emitted program — zig gets a `switch` prong table; cs gets the semantically identical if/else-if chain (first match wins, same contract). C# pattern syntax would carry `and`/`when` words that the host-text lowerer rewrites; the chain needs neither. Emit a Zig char literal for `ch`, escaping the characters that must not appear raw inside a char literal (' and \) and common control chars.