1.6 Results
(require rebellion/base/result) | package: rebellion |
A result is a wrapper around a value that represents either a successful computation or a failed computation. Success results are constructed by wrapping a value with success, and failure results are constructed using failure. The result-case function provides a basic way to unwrap a result. Results are useful when it is difficult to guarantee whether a computation will succeed or fail and specifying a contract up front is either expensive or impossible, such as in filesystem operations, text parsers, or network requests. Wrapping returned values as a result instead of throwing exceptions pushes callers to confront the failure case in order to unwrap the value.
syntax
(result body ...+)
If any body form raises an error, that error is wrapped as a failure result and returned. Any remaining body forms are not evaluated.
procedure
(result-case result #:success success-handler #:failure failure-handler) → any/c result : result? success-handler : (-> any/c any/c) failure-handler : (-> any/c any/c)
> (result-case (success 42) #:success add1 #:failure displayln) 43
> (result-case (failure "oh no!") #:success add1 #:failure displayln) oh no!
procedure
(result/c success-contract failure-contract) → chaperone-contract? success-contract : chaperone-contract? failure-contract : chaperone-contract?
1.6.1 Successful Results
procedure
(success-value succ) → any/c
succ : success?
procedure
(success/c contract) → chaperone-contract?
contract : chaperone-contract?
1.6.2 Failed Results
> (match (failure "kaboom!") [(failure message) (string-upcase message)]) "KABOOM!"
procedure
(failure-error fail) → any/c
fail : failure?
procedure
(failure/c contract) → chaperone-contract?
contract : chaperone-contract?