Table of Contents

Struct Maybe<TSource>

Namespace
Vonage.Common.Monads
Assembly
Vonage.dll

Discriminated union type. Can be in one of two states: Some, or None.

public readonly struct Maybe<TSource>

Type Parameters

TSource

Bound value type.

Inherited Members

Constructors

Maybe()

Constructor for a None.

public Maybe()

Fields

None

Construct a Maybe in a None state.

public static readonly Maybe<TSource> None

Field Value

Maybe<TSource>

Examples

Maybe<string> noValue = Maybe<string>.None;
Console.WriteLine(noValue.IsNone); // true

NullValueMessage

Message indicating Value cannot be null.

public const string NullValueMessage = "Value cannot be null."

Field Value

string

Properties

IsNone

Indicates if in None state.

public bool IsNone { get; }

Property Value

bool

IsSome

Indicates if in Some state.

public bool IsSome { get; }

Property Value

bool

Methods

BindAsync<TDestination>(Func<TSource, Task<Maybe<TDestination>>>)

Monadic bind operation. Chains asynchronous operations that return Maybe, short-circuiting on None.

public Task<Maybe<TDestination>> BindAsync<TDestination>(Func<TSource, Task<Maybe<TDestination>>> bind)

Parameters

bind Func<TSource, Task<Maybe<TDestination>>>

Bind operation.

Returns

Task<Maybe<TDestination>>

Bound functor.

Type Parameters

TDestination

Return type.

Examples

async Task<Maybe<User>> FetchUserAsync(int id) => await userRepository.FindByIdAsync(id);
Maybe<int> userId = 123;
Maybe<User> user = await userId.BindAsync(FetchUserAsync);

Bind<TDestination>(Func<TSource, Maybe<TDestination>>)

Monadic bind operation. Chains operations that return Maybe, short-circuiting on None.

public Maybe<TDestination> Bind<TDestination>(Func<TSource, Maybe<TDestination>> bind)

Parameters

bind Func<TSource, Maybe<TDestination>>

Bind operation.

Returns

Maybe<TDestination>

Bound functor.

Type Parameters

TDestination

Return type.

Examples

Maybe<int> ParseInt(string value) =>
    int.TryParse(value, out var result) ? result : Maybe<int>.None;
Maybe<string> input = "42";
Maybe<int> parsed = input.Bind(ParseInt);
Console.WriteLine(parsed.IsSome); // true

Do(Action<TSource>, Action)

Executes operations depending on the current state.

public Maybe<TSource> Do(Action<TSource> someOperation, Action noneOperation)

Parameters

someOperation Action<TSource>

Some operation.

noneOperation Action

None operation.

Returns

Maybe<TSource>

The current Maybe instance for method chaining.

Examples

Maybe<string> name = "Alice";
name.Do(
    someOperation: value => Console.WriteLine($"Found: {value}"),
    noneOperation: () => Console.WriteLine("Not found")
); // Prints: Found: Alice

DoWhenNone(Action)

Executes an operation if in None state.

public Maybe<TSource> DoWhenNone(Action noneOperation)

Parameters

noneOperation Action

None operation.

Returns

Maybe<TSource>

The current Maybe instance for method chaining.

Examples

Maybe<string> name = Maybe<string>.None;
name.DoWhenNone(() => Console.WriteLine("No value found")); // Prints: No value found

DoWhenSome(Action<TSource>)

Executes an operation if in Some state.

public Maybe<TSource> DoWhenSome(Action<TSource> someOperation)

Parameters

someOperation Action<TSource>

Some operation.

Returns

Maybe<TSource>

The current Maybe instance for method chaining.

Examples

Maybe<string> name = "Alice";
name.DoWhenSome(value => Console.WriteLine($"Hello, {value}!")); // Prints: Hello, Alice!

Equals(object)

public override bool Equals(object obj)

Parameters

obj object

Returns

bool

GetHashCode()

public override int GetHashCode()

Returns

int

GetUnsafe()

Retrieves the Maybe's value. This method is unsafe and will throw an exception if in None state.

public TSource GetUnsafe()

Returns

TSource

The value if in Some state.

Examples

Maybe<string> name = "Alice";
string value = name.GetUnsafe(); // "Alice"

Maybe<string> noName = Maybe<string>.None;
// noName.GetUnsafe(); // Throws NoneStateException

Exceptions

NoneStateException

When in None state.

IfNone(Func<TSource>)

Returns the result of the operation if Maybe is in the None state, the Some value otherwise.

public TSource IfNone(Func<TSource> operation)

Parameters

operation Func<TSource>

Operation to return a value.

Returns

TSource

A value.

Examples

Maybe<string> name = Maybe<string>.None;
string result = name.IfNone(() => "Default Name"); // "Default Name"

IfNone(TSource)

Returns the specified value if Maybe is in the None state, the Some value otherwise.

public TSource IfNone(TSource noneValue)

Parameters

noneValue TSource

The value to return if in None state.

Returns

TSource

A value.

Examples

Maybe<int> count = Maybe<int>.None;
int result = count.IfNone(0); // 0

Maybe<int> hasValue = 42;
int existing = hasValue.IfNone(0); // 42

IfSome(Action<TSource>)

Invokes the action if Maybe is in the Some state, otherwise nothing happens.

public Maybe<TSource> IfSome(Action<TSource> some)

Parameters

some Action<TSource>

Action to invoke

Returns

Maybe<TSource>

The current Maybe instance for method chaining.

Examples

Maybe<string> name = "Alice";
name.IfSome(value => Console.WriteLine($"Hello, {value}!")); // Prints: Hello, Alice!

Maybe<string>.None.IfSome(value => Console.WriteLine(value)); // Does nothing

IfSomeAsync(Func<TSource, Task>)

Invokes the asynchronous action if Maybe is in the Some state, otherwise nothing happens.

public Task<Maybe<TSource>> IfSomeAsync(Func<TSource, Task> some)

Parameters

some Func<TSource, Task>

Asynchronous action to invoke

Returns

Task<Maybe<TSource>>

The current Maybe instance for method chaining.

Examples

Maybe<string> userId = "user-123";
await userId.IfSomeAsync(async id => await notificationService.SendAsync(id));

MapAsync<TDestination>(Func<TSource, Task<TDestination>>)

Projects from one value to another using an asynchronous function.

public Task<Maybe<TDestination>> MapAsync<TDestination>(Func<TSource, Task<TDestination>> map)

Parameters

map Func<TSource, Task<TDestination>>

Asynchronous projection function.

Returns

Task<Maybe<TDestination>>

Mapped functor.

Type Parameters

TDestination

Resulting functor value type.

Examples

Maybe<int> userId = 123;
Maybe<string> userName = await userId.MapAsync(async id => await userService.GetNameAsync(id));

Map<TDestination>(Func<TSource, TDestination>)

Projects from one value to another. Transforms the inner value if Some, otherwise returns None.

public Maybe<TDestination> Map<TDestination>(Func<TSource, TDestination> map)

Parameters

map Func<TSource, TDestination>

Projection function.

Returns

Maybe<TDestination>

Mapped functor.

Type Parameters

TDestination

Resulting functor value type.

Examples

Maybe<string> name = "Alice";
Maybe<int> length = name.Map(n => n.Length); // Some(5)
Maybe<string>.None.Map(n => n.Length); // None

Match<TDestination>(Func<TSource, TDestination>, Func<TDestination>)

Match the two states of the Maybe and return a non-null TDestination.

public TDestination Match<TDestination>(Func<TSource, TDestination> some, Func<TDestination> none)

Parameters

some Func<TSource, TDestination>

Some match operation.

none Func<TDestination>

None match operation.

Returns

TDestination

A non-null TDestination.

Type Parameters

TDestination

Return type.

Examples

Maybe<string> name = "Alice";
string greeting = name.Match(
    some: n => $"Hello, {n}!",
    none: () => "Hello, stranger!"
); // "Hello, Alice!"

Merge<TDestination>(Maybe<TSource>, Func<TSource, TSource, TDestination>)

Merge two maybes together. The merge operation will be used if they're both in a Some state.

public Maybe<TDestination> Merge<TDestination>(Maybe<TSource> other, Func<TSource, TSource, TDestination> merge)

Parameters

other Maybe<TSource>

The other maybe.

merge Func<TSource, TSource, TDestination>

The operation used if they're both in a Some state.

Returns

Maybe<TDestination>

A Maybe.

Type Parameters

TDestination

The return type.

Examples

Maybe<int> first = 10;
Maybe<int> second = 20;
Maybe<int> sum = first.Merge(second, (a, b) => a + b); // Some(30)
Maybe<int>.None.Merge(second, (a, b) => a + b); // None

Some<TDestination>(TDestination)

Construct a Maybe in a Some state.

public static Maybe<TDestination> Some<TDestination>(TDestination value)

Parameters

value TDestination

Value to bind, must be non-null.

Returns

Maybe<TDestination>

Maybe containing Some value.

Type Parameters

TDestination

Bound value type.

Examples

Maybe<string> name = Maybe<string>.Some("Alice");
Console.WriteLine(name.IsSome); // true
// Maybe<string>.Some(null); // Throws InvalidOperationException

Exceptions

InvalidOperationException

Given value is null.

ToResult()

Converts this instance to a Result.

public Result<TSource> ToResult()

Returns

Result<TSource>

A Result with a Success state when Some, or with a Failure state when None.

Examples

Maybe<string> name = "Alice";
Result<string> result = name.ToResult();
Console.WriteLine(result.IsSuccess); // true
Maybe<string>.None.ToResult().IsFailure; // true

ToString()

public override string ToString()

Returns

string

Operators

implicit operator Maybe<TSource>(TSource)

Implicit operator from TSource to Maybe of TSource.

public static implicit operator Maybe<TSource>(TSource value)

Parameters

value TSource

Value to be converted.

Returns

Maybe<TSource>

None if the value is null, Some otherwise.

Examples

Maybe<string> name = "Alice"; // Implicit conversion to Some("Alice")
Maybe<string> noName = null;  // Implicit conversion to None