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
valueT
Returns
Type Parameters
T
None<T>()
Creates a new Maybe<T> with no value.
public static Maybe<T> None<T>()
Returns
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
valueTInfunctionFunc<TIn, Result<TOut>>A function that can validate the input and return 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
TInTOut
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);