Error propagation with ?#
A postfix expression? unwraps ok; on error it returns that error from the enclosing function, which must return a compatible Result. Because it is written after the call, nested calls and pipe steps need no leading keyword.
The following continues the discount and DiscountError declarations in expected failures
:
fn discounted_label(price, percent Decimal) Result[String, DiscountError] {
value := discount(price, percent)?
return ok(f"Discounted price: {value}")
}? on Results and Optionals#
?applies to a Result or an Optional. On a Result it returns the error; on an Optional it returnsnull(or just returns, from aVoidfunction) when the value is absent.- A function cannot mix the two: a Result needs a Result return, an Optional needs an Optional or
Voidreturn. load()?.nameis(load()?).name: unwrap, then readname. There is no?.operator.?does not repeat:f()??is rejected.place[key]?is the in-place borrow in anif varhead, not propagation.
Success and failure paths#
On ok, value holds the Decimal. On error, the function returns that error immediately. Success types may differ (decimal versus text); the error type is forwarded unchanged.
Result[Void, E] can be propagated as a statement. Save this complete test as ready_test.ms and run ms test ready_test.ms:
import expect_equal from "@std/test/test.ms"
fn prepare() Void! => ok()
fn ready_label() String! {
prepare()?
return ok("ready")
}
test "prepare before returning a label" {
expect_equal(ready_label()?, "ready")?
}1 tests, 0 failedStandalone call()? requires a Void success. Otherwise bind it (name := call()?) or return call()?.
Omitting ? discards the Result, which the checker rejects. To ignore it deliberately, use void
.
Propagation in a pipe flow#
A trailing ? on a pipe step in a pipe flow
applies these rules to that step. Given fn label(value Decimal) String, this equals value := discount(...)? then label(value):
fn discounted_label(price, percent Decimal) Result[String, DiscountError] {
return ok(discount(price, percent)? | label)
}Error compatibility#
? requires the operand’s error type to be the enclosing function’s error type.
Result[T, E]can becomeResult[U, E]for the sameE.Errorhas no special conversion: it is an ordinary type.- The original error and its fields pass through unchanged.
- Different error types need an explicit translation, for example
map_erroror pattern matching . - The operand is evaluated once.
operation()? + 1means(operation()?) + 1. - Inside an anonymous function,
?returns from that function.
The compiler rejects ? when:
- its expression is not a Result or an Optional;
- its error type differs from the function’s;
- the enclosing function or test returns Void and the operand is a Result (a
Voidfunction may use?on an Optional); - the operand is an Optional and the function returns neither an Optional nor
Void(a test returns a Result, so?in a test applies to Results only).
See type compatibility .
Cleanup and cancellation#
? does not catch faults or parent cancellation. A propagated error is a return path and performs lexical cleanup
for scopes it leaves.