LEARN VOID / DATA & BEHAVIOR
Enums & pattern matching
Match the shape of an outcome and name what you need.
enum payload:
empty
numbers(items: [i32, 3])
object(item: thing)
struct thing:
value: i32
func make(x: i32) -> result<payload, string>:
if x < 0:
return Err("negative")
return Ok(payload.numbers([x, 2, 3]))
func entry(x: i32) -> i32:
var a: [payload] = [payload.empty, payload.object(thing(value = 5)), payload.numbers([1, 2, 3])]
var copied = a[2]
if copied is payload.numbers(var values):
values[0] = 99
var result = make(x)
match result:
Ok(payload.numbers(var values)):
return values[0] + a.count as i32
_:
return -1Input 5Expected result 8
Give variants their own payload
An enum declares named alternatives such as empty, numbers and object. Each alternative can carry its own typed payload. Only the active variant’s payload is used.
Capture explicitly
var introduces a pattern capture. The is expression checks a pattern, and match selects a branch. Patterns compose, so Ok(payload.numbers(var values)) can inspect a successful result and its enum payload together.
Preserve the data model
Capturing a fixed-array payload copies its value storage. Changing the independent copy does not change the array inside the enum held in the list. Input 5 returns the number payload plus the list count: 8. Incomplete matches can warn; an unmatched match expression raises a runtime failure.