hd-lang

Tour · 7 of 25

Errors are values in the return type, and ? passes them up

A function that can fail returns Result[T, E]: .Ok(value) or .Err(error). The failure is in the signature, so no caller is surprised by a hidden exception. Postfix ? unwraps an .Ok, and on an .Err it returns that error from the current function, so the happy path reads top to bottom. Forget the ? and you are holding a Result, not a number, and the compiler says so. Real code uses an error enum instead of string; a later page shows how.

Run: Ctrl+Enter · Pages: Alt+← Alt+→