Enums & Patterns
Jiang uses enum for a fixed set of named cases. A case can also carry values, which makes the enum
an algebraic data type.
Members may specify values:
Enum case values use Int32 by default. Cases without an explicit value continue from the previous
case.
Use enum [UInt8] or another integer type when the stored representation must be
fixed:
Convert an enum value with a target integer constructor; enum cases do not expose a .value member:
For a no-payload integer enum, Type.init?(integer) returns the matching case or .none when the
integer is not a declared case value:
Payload Enums
Section titled “Payload Enums”A case can carry more than one value. Names make those payload positions easier to document and bind:
Payload cases can also have an explicit integer type and values:
Cases must come before methods and nested types. Use ; to separate the cases from those members:
A payload follows the usual ownership rules. Moving the enum moves its active payload, borrowing a payload does not move it, and destroying the enum destroys only the payload of its current case.
Put ! after the name for a mutable inferred binding:
Use ref name to borrow an enum payload instead of binding it by value. The omitted type position
means _; write ref T name when an explicit type pattern is useful:
This matters when the payload is large, non-copyable, or should be inspected without moving it.
In patterns, ref! Int value creates a unique mutable Int&! borrow. Write ref Int value! or
ref! Int value! when the resulting reference binding itself must be reassignable.
Optional values use .some(...) and .none:
switch
Section titled “switch”Enum payloads:
Borrowed enum payloads use the same ref sub-pattern:
Optional values:
Pattern Roots
Section titled “Pattern Roots”is and switch branch roots accept:
- optional patterns:
.some(payload)and.none - variant patterns:
.name(...)orType.name(...) - tuple patterns such as
(left, right) - literal patterns such as
null, numbers, chars, and booleans
Bindings and wildcards are sub-patterns. A Tuple payload is passed directly to its variant, so a
(Int, Int) payload matches .pair(left, right). Preserve parentheses only for nested tuples:
.nested((left, right), tail).