Result
Edit on GitHubUtilities for working with the Result data type.
The 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.
1 | let failure = Err("Something bad happened") // Creates an unsuccessful Result containing "Something bad happened" |
Values
Functions and constants included in the Result module.
Result.isOk
Added in 0.2.0
No other changes yet.
Checks if the Result is the Ok
variant.
Parameters:
param | type | description |
---|---|---|
result |
Result<a, b> |
The result to check |
Returns:
type | description |
---|---|
Bool |
true if the Result is the Ok variant or false otherwise |
Result.isErr
Added in 0.2.0
No other changes yet.
Checks if the Result is the Err
variant.
Parameters:
param | type | description |
---|---|---|
result |
Result<a, b> |
The result to check |
Returns:
type | description |
---|---|
Bool |
true if the Result is the Err variant or false otherwise |
Result.toOption
Added in 0.2.0
No other changes yet.
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 |
Returns:
type | description |
---|---|
Option<a> |
Some(value) if the Result is Ok(value) or None if the Result is an Err |
Result.flatMap
Added in 0.2.0
No other changes yet.
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 |
Returns:
type | description |
---|---|
Result<b, c> |
A new Result produced by the mapping function if the variant was Ok or the unmodified Err otherwise |
Result.flatMapErr
Added in 0.2.0
No other changes yet.
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 |
Returns:
type | description |
---|---|
Result<b, c> |
A new Result produced by the mapping function if the variant was Err or the unmodified Ok otherwise |
Result.map
Added in 0.2.0
No other changes yet.
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 |
Returns:
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.mapErr
Added in 0.2.0
No other changes yet.
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 |
Returns:
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.mapWithDefault
Added in 0.2.0
No other changes yet.
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 |
Returns:
type | description |
---|---|
b |
The value produced by the mapping function if the result is of the Ok variant or the default value otherwise |
Result.mapWithDefaultFn
Added in 0.2.0
No other changes yet.
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 |
Returns:
type | description |
---|---|
b |
The value produced by one of the mapping functions |
Result.(||)
Added in 0.6.0
version | changes |
---|---|
0.2.0 | Originally named `or` |
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 |
Returns:
type | description |
---|---|
Result<a, b> |
The first Result if it is the Ok variant or the second Result otherwise |
Result.(&&)
Added in 0.6.0
version | changes |
---|---|
0.2.0 | Originally named `and` |
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 |
Returns:
type | description |
---|---|
Result<a, b> |
The second Result if both are the Ok variant or the first Result otherwise |
Result.peek
Added in 0.2.0
No other changes yet.
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 |
Result.peekOk
Added in 0.2.0
No other changes yet.
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 |
Result.peekErr
Added in 0.2.0
No other changes yet.
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 |
Result.expect
Added in 0.4.0
No other changes yet.
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 |
Returns:
type | description |
---|---|
a |
The unwrapped value if the Result is the Ok variant |
Throws:
Failure(String)
- When the
result
isErr
Examples:
Result.unwrap
Added in 0.4.0
No other changes yet.
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 |
Returns:
type | description |
---|---|
a |
The unwrapped value if the result is the Ok variant |
Throws:
Failure(String)
- When the
result
isErr
Examples: