Class Optional
Various extension and factory methods for constructing optional value.
Inherited Members
Namespace: DotNext
Assembly: DotNext.dll
Syntax
public static class Optional
Methods
| Edit this page View SourceCoalesce<T>(in Optional<T>, in Optional<T>)
Returns the second value if the first is empty.
Declaration
public static ref readonly Optional<T> Coalesce<T>(this in Optional<T> first, in Optional<T> second)
Parameters
Type | Name | Description |
---|---|---|
Optional<T> | first | The first optional value. |
Optional<T> | second | The second optional value. |
Returns
Type | Description |
---|---|
Optional<T> | The second value if the first is empty; otherwise, the first value. |
Type Parameters
Name | Description |
---|---|
T | Type of value. |
Convert<TInput, TOutput>(Task<Optional<TInput>>, Converter<TInput, TOutput>)
If a value is present, apply the provided mapping function to it, and if the result is non-null, return an Optional describing the result. Otherwise returns None.
Declaration
public static Task<Optional<TOutput>> Convert<TInput, TOutput>(this Task<Optional<TInput>> task, Converter<TInput, TOutput> converter)
Parameters
Type | Name | Description |
---|---|---|
Task<Optional<TInput>> | task | The task containing Optional value. |
Converter<TInput, TOutput> | converter | A mapping function to be applied to the value, if present. |
Returns
Type | Description |
---|---|
Task<Optional<TOutput>> | An Optional describing the result of applying a mapping function to the value of this Optional, if a value is present, otherwise None. |
Type Parameters
Name | Description |
---|---|
TInput | The type of stored in the Optional container. |
TOutput | The type of the result of the mapping function. |
Create<T, TMonad>(TMonad)
Converts the monad to Optional<T>.
Declaration
public static Optional<T> Create<T, TMonad>(TMonad value) where TMonad : struct, IOptionMonad<T>
Parameters
Type | Name | Description |
---|---|---|
TMonad | value | The value to convert. |
Returns
Type | Description |
---|---|
Optional<T> | The value represented as Optional<T> or None if there is no value. |
Type Parameters
Name | Description |
---|---|
T | The type of the underlying value. |
TMonad | The type of the monad. |
Flatten<T>(in Optional<Optional<T>>)
Flattens the nested optional value.
Declaration
public static Optional<T> Flatten<T>(this in Optional<Optional<T>> optional)
Parameters
Type | Name | Description |
---|---|---|
Optional<Optional<T>> | optional | The nested optional value. |
Returns
Type | Description |
---|---|
Optional<T> | Flattened value. |
Type Parameters
Name | Description |
---|---|
T | The type of the underlying value. |
GetUnderlyingType(Type)
Returns the underlying type argument of the specified optional type.
Declaration
public static Type? GetUnderlyingType(Type optionalType)
Parameters
Type | Name | Description |
---|---|---|
Type | optionalType | Optional type. |
Returns
Type | Description |
---|---|
Type | Underlying type argument of optional type; otherwise, null. |
If<T>(Task<Optional<T>>, Predicate<T>)
If a value is present, and the value matches the given predicate, return an Optional describing the value, otherwise return an empty Optional.
Declaration
public static Task<Optional<T>> If<T>(this Task<Optional<T>> task, Predicate<T> condition)
Parameters
Type | Name | Description |
---|---|---|
Task<Optional<T>> | task | The task returning optional value. |
Predicate<T> | condition | A predicate to apply to the value, if present. |
Returns
Type | Description |
---|---|
Task<Optional<T>> | An Optional describing the value of this Optional if a value is present and the value matches the given predicate, otherwise an empty Optional. |
Type Parameters
Name | Description |
---|---|
T | Type of the value. |
IsOptional(Type)
Indicates that specified type is optional type.
Declaration
public static bool IsOptional(this Type optionalType)
Parameters
Type | Name | Description |
---|---|---|
Type | optionalType | The type to check. |
Returns
Type | Description |
---|---|
bool |
None<T>()
Returns empty value.
Declaration
public static Optional<T> None<T>()
Returns
Type | Description |
---|---|
Optional<T> | The empty value. |
Type Parameters
Name | Description |
---|---|
T | The type of empty result. |
Null<T>()
Wraps null value to Optional<T> container.
Declaration
public static Optional<T> Null<T>() where T : class?
Returns
Type | Description |
---|---|
Optional<T> | The Optional<T> instance representing null value. |
Type Parameters
Name | Description |
---|---|
T | The reference type. |
OrDefault<T>(Task<Optional<T>>)
If a value is present, returns the value, otherwise return default value.
Declaration
public static Task<T?> OrDefault<T>(this Task<Optional<T>> task)
Parameters
Type | Name | Description |
---|---|---|
Task<Optional<T>> | task | The task returning optional value. |
Returns
Type | Description |
---|---|
Task<T> | The value, if present, otherwise default. |
Type Parameters
Name | Description |
---|---|
T | Type of the value. |
OrInvoke<T>(Task<Optional<T>>, Func<T>)
Returns the value if present; otherwise invoke delegate.
Declaration
public static Task<T> OrInvoke<T>(this Task<Optional<T>> task, Func<T> defaultFunc)
Parameters
Type | Name | Description |
---|---|---|
Task<Optional<T>> | task | The task returning optional value. |
Func<T> | defaultFunc | A delegate to be invoked if value is not present. |
Returns
Type | Description |
---|---|
Task<T> | The value, if present, otherwise returned from delegate. |
Type Parameters
Name | Description |
---|---|
T | Type of the value. |
OrNull<T>(in Optional<T>)
If a value is present, returns the value, otherwise null.
Declaration
public static T? OrNull<T>(this in Optional<T> value) where T : struct
Parameters
Type | Name | Description |
---|---|---|
Optional<T> | value | Optional value. |
Returns
Type | Description |
---|---|
T? | Nullable value. |
Type Parameters
Name | Description |
---|---|
T | Value type. |
OrNull<T>(Task<Optional<T>>)
If a value is present, returns the value, otherwise null.
Declaration
public static Task<T?> OrNull<T>(this Task<Optional<T>> task) where T : struct
Parameters
Type | Name | Description |
---|---|---|
Task<Optional<T>> | task | The task returning optional value. |
Returns
Type | Description |
---|---|
Task<T?> | Nullable value. |
Type Parameters
Name | Description |
---|---|
T | The type of the value. |
OrThrow<T, TException>(Task<Optional<T>>)
If a value is present, returns the value, otherwise throw exception.
Declaration
public static Task<T> OrThrow<T, TException>(this Task<Optional<T>> task) where TException : Exception, new()
Parameters
Type | Name | Description |
---|---|---|
Task<Optional<T>> | task | The task returning optional value. |
Returns
Type | Description |
---|---|
Task<T> | The value, if present. |
Type Parameters
Name | Description |
---|---|
T | Type of the value. |
TException | Type of exception to throw. |
OrThrow<T, TException>(Task<Optional<T>>, Func<TException>)
If a value is present, returns the value, otherwise throw exception.
Declaration
public static Task<T> OrThrow<T, TException>(this Task<Optional<T>> task, Func<TException> exceptionFactory) where TException : Exception
Parameters
Type | Name | Description |
---|---|---|
Task<Optional<T>> | task | The task returning optional value. |
Func<TException> | exceptionFactory | Exception factory. |
Returns
Type | Description |
---|---|
Task<T> | The value, if present. |
Type Parameters
Name | Description |
---|---|
T | Type of the value. |
TException | Type of exception to throw. |
Or<T>(Task<Optional<T>>, T?)
Returns the value if present; otherwise return default value.
Declaration
public static Task<T?> Or<T>(this Task<Optional<T>> task, T? defaultValue)
Parameters
Type | Name | Description |
---|---|---|
Task<Optional<T>> | task | The task returning optional value. |
T | defaultValue | The value to be returned if there is no value present. |
Returns
Type | Description |
---|---|
Task<T> | The value, if present, otherwise default. |
Type Parameters
Name | Description |
---|---|
T | The type of the value. |
Some<T>(T)
Wraps the value to Optional<T> container.
Declaration
public static Optional<T> Some<T>(T value)
Parameters
Type | Name | Description |
---|---|---|
T | value | The value to be wrapped. |
Returns
Type | Description |
---|---|
Optional<T> | The optional container. |
Type Parameters
Name | Description |
---|---|
T | The type of the value. |
ToOptional<T>(in T?)
Constructs optional value from nullable reference type.
Declaration
public static Optional<T> ToOptional<T>(this in T? value) where T : struct
Parameters
Type | Name | Description |
---|---|---|
T? | value | The value to convert. |
Returns
Type | Description |
---|---|
Optional<T> | The value wrapped into Optional container. |
Type Parameters
Name | Description |
---|---|
T | Type of value. |