Class MaybeExtensions
- Namespace
- FunctionalDdd
- Assembly
- FunctionalDdd.RailwayOrientedProgramming.dll
Provides extension methods for converting Maybe<T> instances to Result<TValue> objects and for wrapping values in a Result<TValue>.
public static class MaybeExtensions
- Inheritance
-
MaybeExtensions
- Inherited Members
Remarks
These extension methods enable seamless transitions between optional and result-based value representations, supporting functional programming patterns. They help unify error handling and value presence checks in codebases that use both Maybe<T> and Result<TValue> types.
Methods
AsMaybe<T>(in T?)
public static Maybe<T> AsMaybe<T>(this in T? value) where T : struct
Parameters
valueT?
Returns
- Maybe<T>
Returns the Maybe<T> equivalent to the Nullable<T>.
Type Parameters
T
AsMaybe<T>(T)
Wraps the class instance in a Maybe<T>.
public static Maybe<T> AsMaybe<T>(this T value) where T : class
Parameters
valueT
Returns
- Maybe<T>
Returns None<T>() if the class instance is null, otherwise returns From<T>(T?).
Type Parameters
T
AsNullable<T>(in Maybe<T>)
public static T? AsNullable<T>(this in Maybe<T> value) where T : struct
Parameters
valueMaybe<T>
Returns
- T?
Returns the Nullable<T> equivalent to the Maybe<T>.
Type Parameters
T
ToResult<TValue>(in Maybe<TValue>, Error)
Converts a Maybe<T> to a Result<TValue>. If the Maybe has a value, returns success; otherwise, returns failure with the specified error.
public static Result<TValue> ToResult<TValue>(this in Maybe<TValue> maybe, Error error) where TValue : notnull
Parameters
maybeMaybe<TValue>The Maybe instance to convert.
errorErrorThe error to return if the Maybe has no value.
Returns
- Result<TValue>
A Result containing the Maybe's value or the specified error.
Type Parameters
TValueType of the value contained in Maybe.
ToResult<TValue>(in Maybe<TValue>, Func<Error>)
Converts a Maybe<T> to a Result<TValue> using a function to create the error. If the Maybe has a value, returns success; otherwise, returns failure with an error from the function.
public static Result<TValue> ToResult<TValue>(this in Maybe<TValue> maybe, Func<Error> ferror) where TValue : notnull
Parameters
maybeMaybe<TValue>The Maybe instance to convert.
ferrorFunc<Error>A function that produces the error if the Maybe has no value.
Returns
- Result<TValue>
A Result containing the Maybe's value or an error from the function.
Type Parameters
TValueType of the value contained in Maybe.
ToResult<TValue>(TValue)
Wraps a value in a Result<TValue> as a success.
public static Result<TValue> ToResult<TValue>(this TValue value)
Parameters
valueTValueThe value to wrap.
Returns
- Result<TValue>
A successful Result containing the value.
Type Parameters
TValueThe type of the value.