Table of Contents

Class Maybe

Namespace
Trellis
Assembly
Trellis.Core.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.

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.

OptionalNonBlank<TOut>(string?, Func<string, Result<TOut>>)

Validates an optional string, treating null, empty, or whitespace-only input as absence.

public static Result<Maybe<TOut>> OptionalNonBlank<TOut>(string? value, Func<string, Result<TOut>> function) where TOut : notnull

Parameters

value string

The optional string. Nonblank input is passed unchanged to the function.

function Func<string, Result<TOut>>

A validation function invoked exactly once for nonblank input, and never for blank input.

Returns

Result<Maybe<TOut>>

Success with None for blank input; otherwise the function's successful value wrapped in Maybe<T>, or its failure.

Type Parameters

TOut

The validated output type.

Remarks

Blankness follows IsNullOrWhiteSpace(string). This method does not trim nonblank input. Factory failures retain their persist-on-failure intent, and exceptions propagate. Unlike this opt-in helper, Maybe.Optional treats only null as absence.

Exceptions

ArgumentNullException

function is null, even when the input is blank.

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.Ok(Maybe<TOut>.None).

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);

Exceptions

ArgumentNullException

function is null.

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.Ok(Maybe<TOut>.None).

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);

Exceptions

ArgumentNullException

function is null.