Clone
1
Language Patterns
erick-alcachofa edited this page 2025-12-30 03:08:14 +00:00
Pattern Unwrapping & Binding
artichoke supports unwrapping Result and optional values directly within
control-flow constructs. This section describes the available patterns.
if / else
if (foo()) |ok_val| {
/* Ok branch */
} else |err_val| {
/* Err branch */
}
- Using
|name|after the condition binds the success value (or error value in theelsebranch) for the scope of that block. - Works with any type that returns
Resultor?(optional) values.
while Patterns
while (foo()) |ok_val| {
/* Loop continues while Ok */
} else |err_val| {
/* Executes on Err */
}
while (foo.next()) |item| {
/* Iterator-style loop until optional becomes empty */
}
- The first form keeps looping while the expression yields
Ok. - The iterator-style variant continues while the optional contains a value.
match Cases
match (foo()) {
Result::<i32, []u8>::Ok |v| -> {
std::io::print("Success!");
}
_ -> { /* Default */ }
}
- Cases accept type expressions and optional bindings (
|v|). _handles the default/remaining patterns.
Range Loop Binding
for (let element := returns_range_function()) {
/* element is bound for each iteration */
}
- Range loops bind the element name chosen in the header.
Labels
outer_loop := while (condition) {
inner_loop := for (let i = 0; i < 10; i += 1) {
if (i == 5) { break outer_loop; }
}
}
- Labels let you control nested loops using
break label;orcontinue label;.
Error Reporting
- When an unwrap clause is malformed (missing pipes, invalid identifier) the parser emits diagnostics indicating the expected syntax, helping align code with the documented patterns.
These patterns appear throughout typical artichoke code and are supported by the
current parser.