Class OptionExtensions
Extension methods for the Option<T> type.
Inherited Members
Namespace: RustyOptions
Assembly: RustyOptions.dll
Syntax
public static class OptionExtensions
Methods
| Edit this page View SourceAndThen<T1, T2>(Option<T1>, Func<T1, Option<T2>>)
Returns None if the option is None, otherwise calls thenFn with the wrapped value and returns the result.
Declaration
public static Option<T2> AndThen<T1, T2>(this Option<T1> self, Func<T1, Option<T2>> thenFn) where T1 : notnull where T2 : notnull
Parameters
| Type | Name | Description |
|---|---|---|
| Option<T1> | self | The first option. |
| Func<T1, Option<T2>> | thenFn | The function to call with the contained value, if there is a contained value. |
Returns
| Type | Description |
|---|---|
| Option<T2> |
Type Parameters
| Name | Description |
|---|---|
| T1 | The type contained by the first option. |
| T2 | The type contained by the second option. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | Thrown if |
And<T1, T2>(Option<T1>, Option<T2>)
Returns None if self is None, otherwise returns other.
Declaration
public static Option<T2> And<T1, T2>(this Option<T1> self, Option<T2> other) where T1 : notnull where T2 : notnull
Parameters
| Type | Name | Description |
|---|---|---|
| Option<T1> | self | The first option. |
| Option<T2> | other | The second option. |
Returns
| Type | Description |
|---|---|
| Option<T2> |
Type Parameters
| Name | Description |
|---|---|
| T1 | The type contained by the first option. |
| T2 | The type contained by the second option. |
AsOption<T>(T?)
Wraps the given value in an Option<T>.
NOTE: Null values will be returned as None, while non-null values will be returned as Some.
Declaration
public static Option<T> AsOption<T>(this T? value) where T : struct
Parameters
| Type | Name | Description |
|---|---|---|
| T? | value |
Returns
| Type | Description |
|---|---|
| Option<T> | The value wrapped in an Option<T> |
Type Parameters
| Name | Description |
|---|---|
| T |
AsOption<T>(T?)
Wraps the given value in an Option<T>.
NOTE: Null values will be returned as None, while non-null values will be returned as Some.
Declaration
public static Option<T> AsOption<T>(this T? value) where T : class
Parameters
| Type | Name | Description |
|---|---|---|
| T | value |
Returns
| Type | Description |
|---|---|
| Option<T> | The value wrapped in an Option<T> |
Type Parameters
| Name | Description |
|---|---|
| T |
Expect<T>(Option<T>, string)
Returns the contained Some value, or throws an InvalidOperationException
if the value is None.
Declaration
public static T Expect<T>(this Option<T> self, string message) where T : notnull
Parameters
| Type | Name | Description |
|---|---|---|
| Option<T> | self | The option to unwrap. |
| string | message | The message for the exception that gets thrown if the option has no value. |
Returns
| Type | Description |
|---|---|
| T | The value contained in the option. |
Type Parameters
| Name | Description |
|---|---|
| T | The type of the option. |
Exceptions
| Type | Condition |
|---|---|
| InvalidOperationException | Thrown if the option does not contain a value. |
Filter<T>(Option<T>, Func<T, bool>)
Returns None if the option is None, otherwise calls predicate
and returns Some if the predicated returns true, otherwise None.
Declaration
public static Option<T> Filter<T>(this Option<T> self, Func<T, bool> predicate) where T : notnull
Parameters
| Type | Name | Description |
|---|---|---|
| Option<T> | self | The option to check. |
| Func<T, bool> | predicate | The function that determines if the value in the option is valid to return. |
Returns
| Type | Description |
|---|---|
| Option<T> |
|
Type Parameters
| Name | Description |
|---|---|
| T | The type of the value. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | Thrown if |
Flatten<T>(Option<Option<T>>)
Removes one level of nesting from nested options.
Declaration
public static Option<T> Flatten<T>(this Option<Option<T>> self) where T : notnull
Parameters
| Type | Name | Description |
|---|---|---|
| Option<Option<T>> | self | The nested option to flatten. |
Returns
| Type | Description |
|---|---|
| Option<T> | The given option with one level of nesting removed. |
Type Parameters
| Name | Description |
|---|---|
| T | The type of the value. |
MapOrElse<T1, T2>(Option<T1>, Func<T1, T2>, Func<T2>)
Computes a default function result (if None), or applies a different function to the contained value (if Some).
Declaration
public static T2 MapOrElse<T1, T2>(this Option<T1> self, Func<T1, T2> mapper, Func<T2> defaultFactory) where T1 : notnull where T2 : notnull
Parameters
| Type | Name | Description |
|---|---|---|
| Option<T1> | self | The option to map. |
| Func<T1, T2> | mapper | The function that maps the value contained in the option. |
| Func<T2> | defaultFactory | The function that lazily generates a default value, if required. |
Returns
| Type | Description |
|---|---|
| T2 | The mapped value, or the default value. |
Type Parameters
| Name | Description |
|---|---|
| T1 | The type of the option's value. |
| T2 | The return type after mapping. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | Thrown if |
MapOr<T1, T2>(Option<T1>, Func<T1, T2>, T2)
Returns the provided default result (if None), or applies a function to the contained value (if Some).
Arguments passed to MapOr<T1, T2>(Option<T1>, Func<T1, T2>, T2) are eagerly evaluated; if you are passing the result of a function call, it is recommended to use MapOrElse<T1, T2>(Option<T1>, Func<T1, T2>, Func<T2>), which is lazily evaluated.
Declaration
public static T2 MapOr<T1, T2>(this Option<T1> self, Func<T1, T2> mapper, T2 defaultValue) where T1 : notnull where T2 : notnull
Parameters
| Type | Name | Description |
|---|---|---|
| Option<T1> | self | The option to map. |
| Func<T1, T2> | mapper | The function that maps the value contained in the option. |
| T2 | defaultValue | The default value to return if the option is |
Returns
| Type | Description |
|---|---|
| T2 | The mapped value, or the default value. |
Type Parameters
| Name | Description |
|---|---|
| T1 | The type of the option's value. |
| T2 | The return type after mapping. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | Thrown if |
Map<T1, T2>(Option<T1>, Func<T1, T2>)
If the option has a value, passes that option to the mapper function and returns that value
as a Some.
Declaration
public static Option<T2> Map<T1, T2>(this Option<T1> self, Func<T1, T2> mapper) where T1 : notnull where T2 : notnull
Parameters
| Type | Name | Description |
|---|---|---|
| Option<T1> | self | The option to map. |
| Func<T1, T2> | mapper | The function that maps the value contained in the option. |
Returns
| Type | Description |
|---|---|
| Option<T2> | The mapped value as |
Type Parameters
| Name | Description |
|---|---|
| T1 | The type of the option. |
| T2 | The type returned by the mapper functions. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | Thrown if |
None<T>(T)
Returns None option typed to match value. The value is not
used except to determine the type of the option.
Declaration
public static Option<T> None<T>(this T value) where T : notnull
Parameters
| Type | Name | Description |
|---|---|---|
| T | value | The value used to determine the type of the returned |
Returns
| Type | Description |
|---|---|
| Option<T> | A |
Type Parameters
| Name | Description |
|---|---|
| T | The type used in the returned Option<T>. |
OrElse<T>(Option<T>, Func<Option<T>>)
Returns self if it contains a value, otherwise calls elseFn and returns the result.
Declaration
public static Option<T> OrElse<T>(this Option<T> self, Func<Option<T>> elseFn) where T : notnull
Parameters
| Type | Name | Description |
|---|---|---|
| Option<T> | self | The option. |
| Func<Option<T>> | elseFn | The function that creates the alternate value if the option is |
Returns
| Type | Description |
|---|---|
| Option<T> |
Type Parameters
| Name | Description |
|---|---|
| T | The type contained by the option. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | Thrown if |
Or<T>(Option<T>, Option<T>)
Returns self if it contains a value, otherwise returns other.
Arguments passed to or are eagerly evaluated; if you are passing the result of a function call, it is recommended to use OrElse<T>(Option<T>, Func<Option<T>>), which is lazily evaluated.
Declaration
public static Option<T> Or<T>(this Option<T> self, Option<T> other) where T : notnull
Parameters
| Type | Name | Description |
|---|---|---|
| Option<T> | self | The first option. |
| Option<T> | other | The replacement option to use if the first option is |
Returns
| Type | Description |
|---|---|
| Option<T> |
Type Parameters
| Name | Description |
|---|---|
| T | The type contained by the option. |
Some<T>(T)
Wraps the given value in an Option<T>.
NOTE: Null values will be returned as None, while non-null values will be returned as Some.
Declaration
public static Option<T> Some<T>(this T value) where T : notnull
Parameters
| Type | Name | Description |
|---|---|---|
| T | value | The value that will be contained in the |
Returns
| Type | Description |
|---|---|
| Option<T> | The value wrapped in an Option<T> |
Type Parameters
| Name | Description |
|---|---|
| T | The type used in the returned Option<T>. |
UnwrapOrElse<T>(Option<T>, Func<T>)
Returns the contained Some value or computes a default
using the provided defaultFactory.
Declaration
public static T UnwrapOrElse<T>(this Option<T> self, Func<T> defaultFactory) where T : notnull
Parameters
| Type | Name | Description |
|---|---|---|
| Option<T> | self | The option to unwrap. |
| Func<T> | defaultFactory | A function that returns a default value to use if the option is |
Returns
| Type | Description |
|---|---|
| T |
Type Parameters
| Name | Description |
|---|---|
| T | The type of the option. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | Thrown if |
UnwrapOr<T>(Option<T>, T)
Returns the contained Some value or a provided default.
Declaration
public static T UnwrapOr<T>(this Option<T> self, T defaultValue) where T : notnull
Parameters
| Type | Name | Description |
|---|---|---|
| Option<T> | self | The option to bind. |
| T | defaultValue | The default value to return if the option is |
Returns
| Type | Description |
|---|---|
| T |
Type Parameters
| Name | Description |
|---|---|
| T | The type of the option. |
Xor<T>(Option<T>, Option<T>)
Returns Some if exactly one of self, other is Some, otherwise returns None.
Declaration
public static Option<T> Xor<T>(this Option<T> self, Option<T> other) where T : notnull
Parameters
| Type | Name | Description |
|---|---|---|
| Option<T> | self | The option. |
| Option<T> | other | The other option. |
Returns
| Type | Description |
|---|---|
| Option<T> |
Type Parameters
| Name | Description |
|---|---|
| T | The type contained by the option. |
ZipWith<T1, T2, T3>(Option<T1>, Option<T2>, Func<T1, T2, T3>)
Zips this Option and another Option with function zipper.
If this option is Some(s) and other is Some(o), this method returns Some(zipper(s, o)). Otherwise, None is returned.
Declaration
public static Option<T3> ZipWith<T1, T2, T3>(this Option<T1> self, Option<T2> other, Func<T1, T2, T3> zipper) where T1 : notnull where T2 : notnull where T3 : notnull
Parameters
| Type | Name | Description |
|---|---|---|
| Option<T1> | self | The first option. |
| Option<T2> | other | The second option. |
| Func<T1, T2, T3> | zipper | A functon that combines values from the two options into a new type. |
Returns
| Type | Description |
|---|---|
| Option<T3> | An option contianing the result of passing both values to the |
Type Parameters
| Name | Description |
|---|---|
| T1 | The type contained by the first option. |
| T2 | The type contained by the second option. |
| T3 | The type returned by the |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | Thrown if |
Zip<T1, T2>(Option<T1>, Option<T2>)
Zips this Option with another Option.
If this option is Some(s) and other is Some(o), this method returns Some((s, o)). Otherwise, None is returned.
Declaration
public static Option<(T1, T2)> Zip<T1, T2>(this Option<T1> self, Option<T2> other) where T1 : notnull where T2 : notnull
Parameters
| Type | Name | Description |
|---|---|---|
| Option<T1> | self | The first option. |
| Option<T2> | other | The second option. |
Returns
| Type | Description |
|---|---|
| Option<(T1, T2)> | An option containing the values from both input options, if both have values. Otherwise, |
Type Parameters
| Name | Description |
|---|---|
| T1 | The type contained by the first option. |
| T2 | The type contained by the second option. |