Result
Utilities for working with the Result data type.
Edit on GitHubThe Result type is an enum that represents the possibility of a success case (with the Ok variant),
or an error case (with the Err variant). Use a Result as the return type of a function that may return an error.
Added in 0.2.0
No other changes yet.
from "result" include Result
let success = Ok((x) => 1 + x) // Creates a successful Result containing (x) => 1 + x
let failure = Err("Something bad happened") // Creates an unsuccessful Result containing "Something bad happened"
Functions and constants included in the Result module.
Added in 0.2.0
No other changes yet.
isOk : (result: Result<a, b>) => Bool
Checks if the Result is the Ok variant.
Parameters:
| param | type | description |
|---|---|---|
result | Result<a, b> | The result to check |
result: The result to checkReturns:
| type | description |
|---|---|
Bool | true if the Result is the Ok variant or false otherwise |
Bool: true if the Result is the Ok variant or false otherwiseAdded in 0.2.0
No other changes yet.
isErr : (result: Result<a, b>) => Bool
Checks if the Result is the Err variant.
Parameters:
| param | type | description |
|---|---|---|
result | Result<a, b> | The result to check |
result: The result to checkReturns:
| type | description |
|---|---|
Bool | true if the Result is the Err variant or false otherwise |
Bool: true if the Result is the Err variant or false otherwiseAdded in 0.2.0
No other changes yet.
toOption : (result: Result<a, b>) => Option<a>
Converts the Result to an Option. An error value is discarded and replaced with None.
Parameters:
| param | type | description |
|---|---|---|
result | Result<a, b> | The result to convert |
result: The result to convertReturns:
| type | description |
|---|---|
Option<a> | Some(value) if the Result is Ok(value) or None if the Result is an Err |
Option<a>: Some(value) if the Result is Ok(value) or None if the Result is an ErrAdded in 0.2.0
No other changes yet.
flatMap : (fn: (a => Result<b, c>), result: Result<a, c>) => Result<b, c>
If the Result is Ok(value), applies the given function to the value to produce a new Result.
Parameters:
| param | type | description |
|---|---|---|
fn | a => Result<b, c> | The function to call on the value of an Ok variant |
result | Result<a, c> | The result to map |
fn: The function to call on the value of an Ok variantresult: The result to mapReturns:
| type | description |
|---|---|
Result<b, c> | A new Result produced by the mapping function if the variant was Ok or the unmodified Err otherwise |
Result<b, c>: A new Result produced by the mapping function if the variant was Ok or the unmodified Err otherwiseAdded in 0.2.0
No other changes yet.
flatMapErr : (fn: (a => Result<b, c>), result: Result<b, a>) => Result<b, c>
If the Result is an Err(value), applies the given function to the value to produce a new Result.
Parameters:
| param | type | description |
|---|---|---|
fn | a => Result<b, c> | The function to call on the value of an Err variant |
result | Result<b, a> | The result to map |
fn: The function to call on the value of an Err variantresult: The result to mapReturns:
| type | description |
|---|---|
Result<b, c> | A new Result produced by the mapping function if the variant was Err or the unmodified Ok otherwise |
Result<b, c>: A new Result produced by the mapping function if the variant was Err or the unmodified Ok otherwiseAdded in 0.2.0
No other changes yet.
map : (fn: (a => b), result: Result<a, c>) => Result<b, c>
If the Result is Ok(value), applies the given function to the value and wraps the new value in an Ok variant.
Parameters:
| param | type | description |
|---|---|---|
fn | a => b | The function to call on the value of an Ok variant |
result | Result<a, c> | The result to map |
fn: The function to call on the value of an Ok variantresult: The result to mapReturns:
| type | description |
|---|---|
Result<b, c> | A new Ok variant produced by the mapping function if the variant was Ok or the unmodified Err otherwise |
Result<b, c>: A new Ok variant produced by the mapping function if the variant was Ok or the unmodified Err otherwiseAdded in 0.2.0
No other changes yet.
mapErr : (fn: (a => b), result: Result<c, a>) => Result<c, b>
If the Result is Err(value), applies the given function to the value and wraps the new value in an Err variant.
Parameters:
| param | type | description |
|---|---|---|
fn | a => b | The function to call on the value of an Err variant |
result | Result<c, a> | The result to map |
fn: The function to call on the value of an Err variantresult: The result to mapReturns:
| type | description |
|---|---|
Result<c, b> | A new Err variant produced by the mapping function if the variant was Err or the unmodified Ok otherwise |
Result<c, b>: A new Err variant produced by the mapping function if the variant was Err or the unmodified Ok otherwiseAdded in 0.2.0
No other changes yet.
mapWithDefault : (fn: (a => b), def: b, result: Result<a, c>) => b
If the Result is Ok(value), applies the given function to the value to produce a new value, otherwise uses the default value.
Useful for unwrapping a successful Result while providing a fallback for any errors that can occur.
Parameters:
| param | type | description |
|---|---|---|
fn | a => b | The function to call on the value of an Ok variant |
def | b | A fallback value for an Err variant |
result | Result<a, c> | The result to map |
fn: The function to call on the value of an Ok variantdef: A fallback value for an Err variantresult: The result to mapReturns:
| type | description |
|---|---|
b | The value produced by the mapping function if the result is of the Ok variant or the default value otherwise |
b: The value produced by the mapping function if the result is of the Ok variant or the default value otherwiseAdded in 0.2.0
No other changes yet.
mapWithDefaultFn :
(fnOk: (a => b), fnErr: (c => b), result: Result<a, c>) => b
If the Result is Ok(value), applies the fnOk function to the value to produce a new value.
If the Result is Err(value), applies the fnErr function to the value to produce a new value.
Useful for unwrapping a Result into a value, whether it is successful or unsuccessful.
Parameters:
| param | type | description |
|---|---|---|
fnOk | a => b | The function to call on the value of an Ok variant |
fnErr | c => b | The function to call on the value of an Err variant |
result | Result<a, c> | The result to map |
fnOk: The function to call on the value of an Ok variantfnErr: The function to call on the value of an Err variantresult: The result to mapReturns:
| type | description |
|---|---|
b | The value produced by one of the mapping functions |
b: The value produced by one of the mapping functionsAdded in 0.6.0
| version | changes |
|---|---|
0.2.0 | Originally named `or` |
(||) : (result1: Result<a, b>, result2: Result<a, b>) => Result<a, b>
Behaves like a logical OR (||) where the first Result is only returned if it is the Ok variant and falling back to the second Result in all other cases.
Parameters:
| param | type | description |
|---|---|---|
result1 | Result<a, b> | The first result |
result2 | Result<a, b> | The second result |
result1: The first resultresult2: The second resultReturns:
| type | description |
|---|---|
Result<a, b> | The first Result if it is the Ok variant or the second Result otherwise |
Result<a, b>: The first Result if it is the Ok variant or the second Result otherwiseAdded in 0.6.0
| version | changes |
|---|---|
0.2.0 | Originally named `and` |
(&&) : (result1: Result<a, b>, result2: Result<a, b>) => Result<a, b>
Behaves like a logical AND (&&) where the first Result is only returned if it is the Err variant and falling back to the second Result in all other cases.
Parameters:
| param | type | description |
|---|---|---|
result1 | Result<a, b> | The first result |
result2 | Result<a, b> | The second result |
result1: The first resultresult2: The second resultReturns:
| type | description |
|---|---|
Result<a, b> | The second Result if both are the Ok variant or the first Result otherwise |
Result<a, b>: The second Result if both are the Ok variant or the first Result otherwiseAdded in 0.2.0
No other changes yet.
peek : (fnOk: (a => b), fnErr: (c => d), result: Result<a, c>) => Void
If the Result is Ok(value), applies the fnOk function to the value without producing a new value.
If the Result is Err(value), applies the fnErr function to the value without producing a new value.
Useful for inspecting Results without changing anything.
Parameters:
| param | type | description |
|---|---|---|
fnOk | a => b | The function to call on the value of an Ok variant |
fnErr | c => d | The function to call on the value of an Err variant |
result | Result<a, c> | The result to inspect |
fnOk: The function to call on the value of an Ok variantfnErr: The function to call on the value of an Err variantresult: The result to inspectAdded in 0.2.0
No other changes yet.
peekOk : (fn: (a => b), result: Result<a, c>) => Void
If the Result is Ok(value), applies the given function to the value without producing a new value.
Parameters:
| param | type | description |
|---|---|---|
fn | a => b | The function to call on the value of an Ok variant |
result | Result<a, c> | The result to inspect |
fn: The function to call on the value of an Ok variantresult: The result to inspectAdded in 0.2.0
No other changes yet.
peekErr : (fn: (a => b), result: Result<c, a>) => Void
If the Result is Err(value), applies the given function to the value without producing a new value.
Parameters:
| param | type | description |
|---|---|---|
fn | a => b | The function to call on the value of an Err variant |
result | Result<c, a> | The result to inspect |
fn: The function to call on the value of an Err variantresult: The result to inspectAdded in 0.4.0
No other changes yet.
expect : (msg: String, result: Result<a, b>) => a
Extracts the value inside an Ok result, otherwise throw an
exception containing the message and contents of the Err.
Parameters:
| param | type | description |
|---|---|---|
msg | String | The message to prepend if the result contains an Err |
result | Result<a, b> | The result to extract a value from |
msg: The message to prepend if the result contains an Errresult: The result to extract a value fromReturns:
| type | description |
|---|---|
a | The unwrapped value if the Result is the Ok variant |
a: The unwrapped value if the Result is the Ok variantThrows:
Failure(String)
- When the
resultisErr
Examples:
Result.expect("Unexpected error", Ok(1234)) + 42
Added in 0.4.0
No other changes yet.
unwrap : (result: Result<a, b>) => a
Extracts the value inside an Ok result, otherwise throw an
exception containing a default message and contents of the Err.
Parameters:
| param | type | description |
|---|---|---|
result | Result<a, b> | The result to extract a value from |
result: The result to extract a value fromReturns:
| type | description |
|---|---|
a | The unwrapped value if the result is the Ok variant |
a: The unwrapped value if the result is the Ok variantThrows:
Failure(String)
- When the
resultisErr
Examples:
Result.unwrap(Err("This will throw"))