Table of Contents

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

Converts the Nullable struct to a Maybe<T>.

public static Maybe<T> AsMaybe<T>(this in T? value) where T : struct

Parameters

value T?

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

value T

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

Converts the Maybe<T> to a Nullable struct.

public static T? AsNullable<T>(this in Maybe<T> value) where T : struct

Parameters

value Maybe<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

maybe Maybe<TValue>

The Maybe instance to convert.

error Error

The 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

TValue

Type 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

maybe Maybe<TValue>

The Maybe instance to convert.

ferror Func<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

TValue

Type 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

value TValue

The value to wrap.

Returns

Result<TValue>

A successful Result containing the value.

Type Parameters

TValue

The type of the value.