Class Maybe
- Namespace
- Trellis
- Assembly
- Trellis.Results.dll
Contains static methods to create a Maybe<T> object.
public static class Maybe
- Inheritance
-
Maybe
- Inherited Members
Methods
From<T>(T?)
Creates a new Maybe<T> from a value. If the value is null, creates an empty Maybe.
public static Maybe<T> From<T>(T? value) where T : notnull
Parameters
valueTThe value to wrap. If null, returns None<T>().
Returns
Type Parameters
TThe type of the value. Must be a non-null type.
None<T>()
Creates a new Maybe<T> with no value.
public static Maybe<T> None<T>() where T : notnull
Returns
Type Parameters
TThe type of the value. Must be a non-null type.
Optional<TIn, TOut>(TIn?, Func<TIn, Result<TOut>>)
Converts an optional nullable value type to a strongly typed value object wrapped in Maybe<T>.
public static Result<Maybe<TOut>> Optional<TIn, TOut>(TIn? value, Func<TIn, Result<TOut>> function) where TIn : struct where TOut : notnull
Parameters
valueTIn?The nullable input. If null, returns
Result.Success(Maybe.None<TOut>()).functionFunc<TIn, Result<TOut>>A function that validates the input and returns a Result<TValue>.
Returns
- Result<Maybe<TOut>>
State Return valueis nullMaybe< TOut> without value.valuehas value andfunctionreturned SuccessMaybe< TOut> with value fromfunction.valuehas value andfunctionreturned FailureThe Error from the function.
Type Parameters
TInThe nullable value input type.
TOutThe validated output type.
Examples
int? quantity = 5;
var result = Maybe.Optional(quantity, Quantity.TryCreate);
Optional<TIn, TOut>(TIn?, Func<TIn, Result<TOut>>)
Converts an optional nullable reference type to a strongly typed value object wrapped in Maybe<T>.
public static Result<Maybe<TOut>> Optional<TIn, TOut>(TIn? value, Func<TIn, Result<TOut>> function) where TIn : class where TOut : notnull
Parameters
valueTInThe nullable input. If null, returns
Result.Success(Maybe.None<TOut>()).functionFunc<TIn, Result<TOut>>A function that validates the input and returns a Result<TValue>.
Returns
- Result<Maybe<TOut>>
State Return valueis nullMaybe< TOut> without value.valueis not null andfunctionreturned SuccessMaybe< TOut> with value fromfunction.valueis not null andfunctionreturned FailureThe Error from the function.
Type Parameters
TInThe nullable reference input type.
TOutThe validated output type.
Examples
string? zipCode = "98052";
var result = Maybe.Optional(zipCode, ZipCode.TryCreate);