Table of Contents

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

value T

The value to wrap. If null, returns None<T>().

Returns

Maybe<T>

A Maybe<T> object with the value, or None if null.

Type Parameters

T

The 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

Maybe<T>

Maybe<T> object with no value.

Type Parameters

T

The 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

value TIn?

The nullable input. If null, returns Result.Success(Maybe.None<TOut>()).

function Func<TIn, Result<TOut>>

A function that validates the input and returns a Result<TValue>.

Returns

Result<Maybe<TOut>>
StateReturn
value is nullMaybe<TOut> without value.
value has value and function returned SuccessMaybe<TOut> with value from function.
value has value and function returned FailureThe Error from the function.

Type Parameters

TIn

The nullable value input type.

TOut

The 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

value TIn

The nullable input. If null, returns Result.Success(Maybe.None<TOut>()).

function Func<TIn, Result<TOut>>

A function that validates the input and returns a Result<TValue>.

Returns

Result<Maybe<TOut>>
StateReturn
value is nullMaybe<TOut> without value.
value is not null and function returned SuccessMaybe<TOut> with value from function.
value is not null and function returned FailureThe Error from the function.

Type Parameters

TIn

The nullable reference input type.

TOut

The validated output type.

Examples

string? zipCode = "98052";
var result = Maybe.Optional(zipCode, ZipCode.TryCreate);