Show / Hide Table of Contents

Class OptionResultExtensions

Extension methods that transform Option<T> to Result<T, TErr>, or vice-versa.

Inheritance
object
OptionResultExtensions
Inherited Members
object.Equals(object)
object.Equals(object, object)
object.GetHashCode()
object.GetType()
object.MemberwiseClone()
object.ReferenceEquals(object, object)
object.ToString()
Namespace: RustyOptions
Assembly: RustyOptions.dll
Syntax
public static class OptionResultExtensions

Methods

| Edit this page View Source

Err<T, TErr>(Result<T, TErr>)

Converts from the Err state of Result<T, TErr> to Option<T>.

Declaration
public static Option<TErr> Err<T, TErr>(this Result<T, TErr> self) where T : notnull where TErr : notnull
Parameters
Type Name Description
Result<T, TErr> self

The result to be converted.

Returns
Type Description
Option<TErr>

Some(TErr) if the result is Err, otherwise None.

Type Parameters
Name Description
T

The type of the value.

TErr

The type of the error.

| Edit this page View Source

OkNumber<T, TErr>(Result<T, TErr>)

Converts from the Ok state of Result<T, TErr> to NumericOption<T>.

Declaration
public static NumericOption<T> OkNumber<T, TErr>(this Result<T, TErr> self) where T : struct, INumber<T> where TErr : notnull
Parameters
Type Name Description
Result<T, TErr> self

The result to be converted.

Returns
Type Description
NumericOption<T>

Some(T) if the result is Ok, otherwise None.

Type Parameters
Name Description
T

The type of the value.

TErr

The type of the error.

| Edit this page View Source

OkOrElse<T, TErr>(NumericOption<T>, Func<TErr>)

Transforms the NumericOption<T> into a Result<T, TErr>, mapping Some to Ok and None to Err using the provided errorFactory.

Declaration
public static Result<T, TErr> OkOrElse<T, TErr>(this NumericOption<T> self, Func<TErr> errorFactory) where T : struct, INumber<T> where TErr : notnull
Parameters
Type Name Description
NumericOption<T> self

The option to transform.

Func<TErr> errorFactory

A function that creates an error object to be used if the option is None.

Returns
Type Description
Result<T, TErr>

A Result<T, TErr> that contains either the option's value, or the provided error.

Type Parameters
Name Description
T

The type of the option's value.

TErr

The type of the error.

Exceptions
Type Condition
ArgumentNullException

Thrown if errorFactory is null.

| Edit this page View Source

OkOrElse<T, TErr>(Option<T>, Func<TErr>)

Transforms the Option<T> into a Result<T, TErr>, mapping Some to Ok and None to Err using the provided errorFactory.

Declaration
public static Result<T, TErr> OkOrElse<T, TErr>(this Option<T> self, Func<TErr> errorFactory) where T : notnull where TErr : notnull
Parameters
Type Name Description
Option<T> self

The option to transform.

Func<TErr> errorFactory

A function that creates an error object to be used if the option is None.

Returns
Type Description
Result<T, TErr>

A Result<T, TErr> that contains either the option's value, or the provided error.

Type Parameters
Name Description
T

The type of the option's value.

TErr

The type of the error.

Exceptions
Type Condition
ArgumentNullException

Thrown if errorFactory is null.

| Edit this page View Source

OkOr<T, TErr>(NumericOption<T>, TErr)

Transforms the NumericOption<T> into a Result<T, TErr>, mapping Some to Ok and None to Err using the provided error.

Declaration
public static Result<T, TErr> OkOr<T, TErr>(this NumericOption<T> self, TErr error) where T : struct, INumber<T> where TErr : notnull
Parameters
Type Name Description
NumericOption<T> self

The option to transform.

TErr error

The error to use if the option is None.

Returns
Type Description
Result<T, TErr>

A Result<T, TErr> that contains either the option's value, or the provided error.

Type Parameters
Name Description
T

The type of the option's value.

TErr

The type of the error.

| Edit this page View Source

OkOr<T, TErr>(Option<T>, TErr)

Transforms the Option<T> into a Result<T, TErr>, mapping Some to Ok and None to Err using the provided error.

Declaration
public static Result<T, TErr> OkOr<T, TErr>(this Option<T> self, TErr error) where T : notnull where TErr : notnull
Parameters
Type Name Description
Option<T> self

The option to transform.

TErr error

The error to use if the option is None.

Returns
Type Description
Result<T, TErr>

A Result<T, TErr> that contains either the option's value, or the provided error.

Type Parameters
Name Description
T

The type of the option's value.

TErr

The type of the error.

| Edit this page View Source

Ok<T, TErr>(Result<T, TErr>)

Converts from the Ok state of Result<T, TErr> to Option<T>.

Declaration
public static Option<T> Ok<T, TErr>(this Result<T, TErr> self) where T : notnull where TErr : notnull
Parameters
Type Name Description
Result<T, TErr> self

The result to be converted.

Returns
Type Description
Option<T>

Some(T) if the result is Ok, otherwise None.

Type Parameters
Name Description
T

The type of the value.

TErr

The type of the error.

| Edit this page View Source

Transpose<T, TErr>(Option<Result<T, TErr>>)

Transposes an Option of a Result into a Result of an Option.

None will be mapped to Ok(None). Some(Ok(_)) and Some(Err(_)) will be mapped to Ok(Some(_)) and Err(_).

Declaration
public static Result<Option<T>, TErr> Transpose<T, TErr>(this Option<Result<T, TErr>> self) where T : notnull where TErr : notnull
Parameters
Type Name Description
Option<Result<T, TErr>> self

An option containing a result.

Returns
Type Description
Result<Option<T>, TErr>

An equivalent result containing an option.

Type Parameters
Name Description
T

The type of the value.

TErr

The type of the error.

| Edit this page View Source

Transpose<T, TErr>(Result<Option<T>, TErr>)

Transposes a Result of an Option into an Option of a Result.

Ok(None) will be mapped to None. Ok(Some(_)) and Err(_) will be mapped to Some(Ok(_)) and Some(Err(_)).

Declaration
public static Option<Result<T, TErr>> Transpose<T, TErr>(this Result<Option<T>, TErr> self) where T : notnull where TErr : notnull
Parameters
Type Name Description
Result<Option<T>, TErr> self

A result containing an option.

Returns
Type Description
Option<Result<T, TErr>>

An equivalent option containing a result.

Type Parameters
Name Description
T

The type of the value.

TErr

The type of the error.

  • Edit this page
  • View Source
In this article
Back to top Generated by DocFX