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
valueTThe value to wrap. If null, returns None.
Returns
Type Parameters
TThe 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
valuestringThe optional string. Nonblank input is passed unchanged to the function.
functionFunc<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
TOutThe 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
functionis 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
valueTIn?The nullable input. If null, returns
Result.Ok(Maybe<TOut>.None).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);
Exceptions
- ArgumentNullException
functionis 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
valueTInThe nullable input. If null, returns
Result.Ok(Maybe<TOut>.None).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);
Exceptions
- ArgumentNullException
functionis null.