On this page:
result?
result
result-case
result/  c
1.6.1 Successful Results
success?
success
success-value
success/  c
1.6.2 Failed Results
failure?
failure
failure-error
failure/  c
8.12

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.

procedure

(result? v)  boolean?

  v : any/c
A predicate for result values.

syntax

(result body ...+)

Evaluates each body form and returns the result of the last body, wrapped as a success result. Definitions within the body forms are locally scoped and aren’t visible outside the result expression. The last body must be an expression that produces exactly one value.

Example:
> (result
   (define foo 1)
   (define bar 2)
   (+ foo bar))

(success 3)

If any body form raises an error, that error is wrapped as a failure result and returned. Any remaining body forms are not evaluated.

Example:
> (result
   (define foo 1)
   (raise "oh no!")
   (define bar 2)
   (+ foo bar))

(failure "oh no!")

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)
Unwraps result, then applies either success-handler or failure-handler to the unwrapped value depending on whether result is a successful result or a failed result.

Examples:
> (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?
Constructs a contract that accepts successful result values matching success-contract and failed result values matching failure-contract. Equivalent to (or/c (success/c success-contract) (failure/c failure-contract)).

1.6.1 Successful Results🔗ℹ

procedure

(success? v)  boolean?

  v : any/c
A predicate for successful result values. Implies result?.

procedure

(success v)  success?

  v : any/c
Constructs a successful result. Additionally, success can be used as a match expander with match.

Example:
> (match (success 42)
    [(success x) (add1 x)])

43

procedure

(success-value succ)  any/c

  succ : success?
Returns the value wrapped by succ.

procedure

(success/c contract)  chaperone-contract?

  contract : chaperone-contract?
Constructs a contract that accepts successful result values matching contract.

1.6.2 Failed Results🔗ℹ

procedure

(failure? v)  boolean?

  v : any/c
A predicate for failed result values. Implies result?.

procedure

(failure err)  failure?

  err : any/c
Constructs a failed result. Additionally, failure can be used as a match expander with match.

Example:
> (match (failure "kaboom!")
    [(failure message) (string-upcase message)])

"KABOOM!"

procedure

(failure-error fail)  any/c

  fail : failure?
Returns the error value wrapped by fail.

procedure

(failure/c contract)  chaperone-contract?

  contract : chaperone-contract?
Constructs a contract that accepts failed result values matching contract.