On this page:
result?
result-case
result/  c
1.5.1 Successful Results
success?
success
success-value
success/  c
1.5.2 Failed Results
failure?
failure
failure-error
failure/  c
7.4

1.5 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.

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.5.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.

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.5.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.

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.