ESC
No recent searches
Search by
Standard Library  /  Result

Result

Utilities for working with the Result data type.

Edit on GitHub

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

Values

Functions and constants included in the Result module.

Result.isOk

Added in 0.2.0 No other changes yet.
isOk : (result: Result<a, b>) => Bool

Checks if the Result is the Ok variant.

Parameters:

result: The result to check

Returns:

Bool: true if the Result is the Ok variant or false otherwise

Result.isErr

Added in 0.2.0 No other changes yet.
isErr : (result: Result<a, b>) => Bool

Checks if the Result is the Err variant.

Parameters:

result: The result to check

Returns:

Bool: true if the Result is the Err variant or false otherwise

Result.toOption

Added 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:

result: The result to convert

Returns:

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

fn: The function to call on the value of an Ok variant
result: The result to map

Returns:

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

fn: The function to call on the value of an Err variant
result: The result to map

Returns:

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

fn: The function to call on the value of an Ok variant
result: The result to map

Returns:

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

fn: The function to call on the value of an Err variant
result: The result to map

Returns:

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

fn: The function to call on the value of an Ok variant
def: A fallback value for an Err variant
result: The result to map

Returns:

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

fnOk: The function to call on the value of an Ok variant
fnErr: The function to call on the value of an Err variant
result: The result to map

Returns:

b: The value produced by one of the mapping functions

Result.(||)

Added in 0.6.0
versionchanges
0.2.0Originally 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:

result1: The first result
result2: The second result

Returns:

Result<a, b>: The first Result if it is the Ok variant or the second Result otherwise

Result.(&&)

Added in 0.6.0
versionchanges
0.2.0Originally 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:

result1: The first result
result2: The second result

Returns:

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

fnOk: The function to call on the value of an Ok variant
fnErr: The function to call on the value of an Err variant
result: The result to inspect

Result.peekOk

Added 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:

fn: The function to call on the value of an Ok variant
result: The result to inspect

Result.peekErr

Added 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:

fn: The function to call on the value of an Err variant
result: The result to inspect

Result.expect

Added 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:

msg: The message to prepend if the result contains an Err
result: The result to extract a value from

Returns:

a: The unwrapped value if the Result is the Ok variant

Throws:

Failure(String)

  • When the result is Err

Examples:

Result.expect("Unexpected error", Ok(1234)) + 42

Result.unwrap

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:

result: The result to extract a value from

Returns:

a: The unwrapped value if the result is the Ok variant

Throws:

Failure(String)

  • When the result is Err

Examples:

Result.unwrap(Err("This will throw"))

Sign up for farm-to-inbox developer news

You can unsubscribe at any time. Read our privacy policy.

Copyright © 2024 The Grain Programming Language