handling errors
A function that can fail either throws an exception or returns a Result[T, E]. Return a Result when failure is one of the outcomes the caller has to deal with, such as input that doesn't parse or a setting that isn't there. Throw when the failure is something no caller nearby can act on. Exceptions, throw and try are covered in control flow.
Result
Ghul.Result[T, E] is a union in the runtime library:
union Result[T, E] is
OK(value: T) default
ERROR(error: E)
siOK holds the value the operation produced, and ERROR holds what it failed with. use Ghul.Result brings it into scope. A function returns Result.OK(value) or Result.ERROR(error), and the type arguments of each come from the function's return type.
OK is the union's default variant, so the operators that read an optional's value read a Result's value in the same way:
r?istruewhenrholdsOK. Insideif r?,ris narrowed toOK, sor.valuereads the value directly.r ?? dgives the value, ordwhenrholdsERROR.r?.mreads the membermof the value. The result is aResultholding the member, and anERRORis passed on unchanged.r!gives the value, and throws whenrholdsERROR.
case, if let and isa read either variant by name:
A discarded Result is a warning, discarded-must-use, because its failure would go unseen. Read it with one of the forms above, or write let _ = expr where it is ignored on purpose.
returning an error with !
Inside a function that returns a Result, ! on a Result with the same error type returns the ERROR from the function, instead of throwing. The rest of the function runs only with values that succeeded, so a sequence of steps that can each fail reads as straight-line code. This needs an error type that implements Ghul.Error. A union of the failures a part of the program can produce is the usual choice:
Ghul.Error has one member, message, which gives the error's text unless the type declares its own. Anywhere else, a failed ! throws: a Ghul.ErrorException holding the error when its type implements Ghul.Error, and a Ghul.UnwrapFailedException otherwise. string doesn't implement Ghul.Error, so ! on a Result[int, string] throws wherever it is written.
keeping a result with &|>
r &|> f() passes the value in an OK to f, and wraps what f returns in OK, at the type f returns. A Result holding ERROR skips the call, including its arguments, and is passed on unchanged. When f returns a Result itself, that is the result, so a chain of &|> stages stops at the first call that fails. The chain is still a Result, so it can be stored, returned, or ended with ??:
converting an error with ||>
r ||> f() is the mirror of &|>: it passes the error in an ERROR to f, and wraps what f returns in ERROR. A Result holding OK skips the call. Use it to turn one error type into another, typically to turn the errors of a lower layer into the errors of the code calling it. When f returns a Result, that is the result, so load(path) ||> load_backup() calls load_backup only when load fails.
Write ! after the call to convert the error and return it from the function in one step. When r holds OK, r ||> f()! gives the value and builds nothing:
?, !, ??, ?., &|> and ||> work in the same way on a union of your own that has a default variant: see option-shaped unions. The three thread-first operators are compared in functional programming.