Table of Contents

Class Maybe

Namespace
FunctionalDdd
Assembly
FunctionalDdd.RailwayOrientedProgramming.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> with a value.

public static Maybe<T> From<T>(T? value)

Parameters

value T

Returns

Maybe<T>

A Maybe<T> object with the value.

Type Parameters

T

None<T>()

Creates a new Maybe<T> with no value.

public static Maybe<T> None<T>()

Returns

Maybe<T>

Maybe<T> object with no value.

Type Parameters

T

Optional<TIn, TOut>(TIn?, Func<TIn, Result<TOut>>)

Helps convert optional primitive types to strongly typed object.

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

Parameters

value TIn
function Func<TIn, Result<TOut>>

A function that can validate the input and return 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
TOut

Examples

This code snippet demonstrates how to transform an optional string representing a zipcode into a strongly typed Zipcode Maybe object.

If the string is null, the method returns a Maybe<Zipcode> of None.
Otherwise, it invokes the specified function and, if the function is successful, it wraps the result in a Maybe object.
If the function fails, the method returns the failure. This is useful when you want to transform a string into a strongly typed object with validation.

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