Table of Contents

Struct Result<T>

Namespace
Vonage.Common.Monads
Assembly
Vonage.dll

Represents the result of an operation. Can be in one of two states: Success, or Failure. Use this type to handle operations that may fail without throwing exceptions.

public readonly struct Result<T>

Type Parameters

T

Bound value type.

Inherited Members

Examples

Result<int> success = Result<int>.FromSuccess(42);
Result<int> failure = Result<int>.FromFailure(new ValidationFailure("Invalid input"));
string message = success.Match(
    successOperation: value => $"Got: {value}",
    failureOperation: error => error.GetFailureMessage()
);

Properties

IsFailure

Indicates if in Failure state.

public bool IsFailure { get; }

Property Value

bool

IsSuccess

Indicates if in Success state.

public bool IsSuccess { get; }

Property Value

bool

Methods

BiMap<TB>(Func<T, TB>, Func<IResultFailure, IResultFailure>)

Projects from one value to another for each state of the Monad.

public Result<TB> BiMap<TB>(Func<T, TB> successMap, Func<IResultFailure, IResultFailure> failureMap)

Parameters

successMap Func<T, TB>

Projection function for success state.

failureMap Func<IResultFailure, IResultFailure>

Projection function for failure state.

Returns

Result<TB>

Mapped functor.

Type Parameters

TB

Resulting functor value type.

Examples

Result<int> result = Result<int>.FromSuccess(42);
Result<string> mapped = result.BiMap(
    successMap: value => value.ToString(),
    failureMap: failure => new WrappedFailure(failure)
);

BindAsync<TB>(Func<T, Task<Result<TB>>>)

Monadic bind operation with an asynchronous function.

public Task<Result<TB>> BindAsync<TB>(Func<T, Task<Result<TB>>> bind)

Parameters

bind Func<T, Task<Result<TB>>>

Asynchronous bind operation.

Returns

Task<Result<TB>>

Asynchronous bound functor.

Type Parameters

TB

Return type.

Examples

Result<int> userId = Result<int>.FromSuccess(123);
Result<User> user = await userId.BindAsync(id => FetchUserAsync(id));

Bind<TB>(Func<T, Result<TB>>)

Monadic bind operation. Chains operations that return Result, short-circuiting on Failure.

public Result<TB> Bind<TB>(Func<T, Result<TB>> bind)

Parameters

bind Func<T, Result<TB>>

Bind operation.

Returns

Result<TB>

Bound functor.

Type Parameters

TB

Return type.

Examples

Result<int> Divide(int a, int b) =>
    b == 0 ? Result<int>.FromFailure(new DivisionByZeroFailure()) : a / b;
Result<int> result = Result<int>.FromSuccess(10).Bind(x => Divide(x, 2)); // Success(5)

Do(Action<T>, Action<IResultFailure>)

Executes operations depending on the current state.

public Result<T> Do(Action<T> successOperation, Action<IResultFailure> failureOperation)

Parameters

successOperation Action<T>

Success operation.

failureOperation Action<IResultFailure>

Failure operation.

Returns

Result<T>

The current Result instance for method chaining.

Examples

Result<string> result = GetResultAsync();
result.Do(
    successOperation: value => Console.WriteLine($"Success: {value}"),
    failureOperation: error => Console.WriteLine($"Error: {error.GetFailureMessage()}")
);

DoWhenFailure(Action<IResultFailure>)

Executes an operation if in Failure state.

public Result<T> DoWhenFailure(Action<IResultFailure> failureOperation)

Parameters

failureOperation Action<IResultFailure>

Failure operation.

Returns

Result<T>

The current Result instance for method chaining.

Examples

result.DoWhenFailure(error => logger.LogError(error.GetFailureMessage()));

DoWhenSuccess(Action<T>)

Executes an operation if in Success state.

public Result<T> DoWhenSuccess(Action<T> successOperation)

Parameters

successOperation Action<T>

Success operation.

Returns

Result<T>

The current Result instance for method chaining.

Examples

result.DoWhenSuccess(value => logger.LogInformation($"Got value: {value}"));

Equals(object)

public override bool Equals(object obj)

Parameters

obj object

Returns

bool

FromFailure(IResultFailure)

Construct Result from Failure.

public static Result<T> FromFailure(IResultFailure failure)

Parameters

failure IResultFailure

Failure value.

Returns

Result<T>

Failure Result.

Examples

Result<int> failure = Result<int>.FromFailure(new ValidationFailure("Invalid input"));
Console.WriteLine(failure.IsFailure); // true

FromSuccess(T)

Construct Result from Success.

public static Result<T> FromSuccess(T value)

Parameters

value T

Success value.

Returns

Result<T>

Success Result.

Examples

Result<int> success = Result<int>.FromSuccess(42);
Console.WriteLine(success.IsSuccess); // true

GetFailureUnsafe()

Retrieves the Failure value. This method is unsafe and will throw an exception if in Success state.

public IResultFailure GetFailureUnsafe()

Returns

IResultFailure

The Failure value if in Failure state.

Examples

Result<int> failure = Result<int>.FromFailure(new ValidationFailure("Invalid"));
IResultFailure error = failure.GetFailureUnsafe();
// success.GetFailureUnsafe(); // Throws InvalidOperationException

Exceptions

InvalidOperationException

When Result is not in Failure state.

GetHashCode()

public override int GetHashCode()

Returns

int

GetSuccessUnsafe()

Retrieves the Success value. This method is unsafe and will throw an exception if in Failure state.

public T GetSuccessUnsafe()

Returns

T

The Success value if in Success state.

Examples

Result<int> success = Result<int>.FromSuccess(42);
int value = success.GetSuccessUnsafe(); // 42
// failure.GetSuccessUnsafe(); // Throws exception from IResultFailure.ToException()

IfFailure(Action<IResultFailure>)

Invokes the action if Result is in the Failure state, otherwise nothing happens.

[Obsolete("Use '.DoWhenFailure' instead.")]
public void IfFailure(Action<IResultFailure> action)

Parameters

action Action<IResultFailure>

Action to invoke.

IfFailure(Func<IResultFailure, T>)

Returns the invocation result if the Result is in the Failure state, the success value otherwise.

public T IfFailure(Func<IResultFailure, T> operation)

Parameters

operation Func<IResultFailure, T>

Operation to invoke if the Result is in the Failure state.

Returns

T

The invocation result if the Result is in the Failure state, the success value otherwise.

Examples

Result<int> result = GetResult();
int value = result.IfFailure(error => -1); // Returns -1 if Failure

IfFailure(T)

Returns the default value if the Result is in the Failure state, the success value otherwise.

public T IfFailure(T defaultValue)

Parameters

defaultValue T

Value to return if in the Failure state.

Returns

T

The default value if the Result is in the Failure state, the success value otherwise.

Examples

Result<int> result = GetResult();
int value = result.IfFailure(0); // Returns 0 if Failure, success value otherwise

IfSuccess(Action<T>)

Invokes the action if Result is in the Success state, otherwise nothing happens.

public Result<T> IfSuccess(Action<T> action)

Parameters

action Action<T>

Action to invoke.

Returns

Result<T>

The initial result.

Examples

Result<string> result = GetResult();
result.IfSuccess(value => Console.WriteLine($"Got: {value}"));

IfSuccessAsync(Func<T, Task>)

Invokes the asynchronous action if Result is in the Success state, otherwise nothing happens.

public Task<Result<T>> IfSuccessAsync(Func<T, Task> action)

Parameters

action Func<T, Task>

Asynchronous action to invoke.

Returns

Task<Result<T>>

The initial result.

Examples

Result<string> result = GetResult();
await result.IfSuccessAsync(async value => await SaveAsync(value));

MapAsync<TB>(Func<T, Task<TB>>)

Projects from one value to another using an asynchronous function.

public Task<Result<TB>> MapAsync<TB>(Func<T, Task<TB>> map)

Parameters

map Func<T, Task<TB>>

Asynchronous projection function.

Returns

Task<Result<TB>>

Asynchronous mapped functor.

Type Parameters

TB

Resulting functor value type.

Examples

Result<int> result = Result<int>.FromSuccess(123);
Result<User> user = await result.MapAsync(id => FetchUserAsync(id));

Map<TB>(Func<T, TB>)

Projects from one value to another. Transforms the success value if Success, otherwise returns Failure.

public Result<TB> Map<TB>(Func<T, TB> map)

Parameters

map Func<T, TB>

Projection function.

Returns

Result<TB>

Mapped functor.

Type Parameters

TB

Resulting functor value type.

Examples

Result<int> result = Result<int>.FromSuccess(42);
Result<string> mapped = result.Map(value => value.ToString()); // Success("42")

Match<TB>(Func<T, TB>, Func<IResultFailure, TB>)

Match the two states of the Result and return a non-null TB.

public TB Match<TB>(Func<T, TB> successOperation, Func<IResultFailure, TB> failureOperation)

Parameters

successOperation Func<T, TB>

Success match operation.

failureOperation Func<IResultFailure, TB>

Failure match operation.

Returns

TB

A non-null TB.

Type Parameters

TB

Return type.

Examples

Result<int> result = GetResult();
string message = result.Match(
    successOperation: value => $"Success: {value}",
    failureOperation: error => $"Error: {error.GetFailureMessage()}"
);

Merge<TSource, TDestination>(Result<TSource>, Func<T, TSource, TDestination>)

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

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

Parameters

other Result<TSource>

The other result.

merge Func<T, TSource, TDestination>

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

Returns

Result<TDestination>

A result.

Type Parameters

TSource

The secondary result type.

TDestination

The return type.

Examples

Result<int> first = Result<int>.FromSuccess(10);
Result<int> second = Result<int>.FromSuccess(20);
Result<int> sum = first.Merge(second, (a, b) => a + b); // Success(30)

ToString()

public override string ToString()

Returns

string

Operators

implicit operator Result<T>(T)

Implicit operator from T to Result of T.

public static implicit operator Result<T>(T value)

Parameters

value T

Value to be converted.

Returns

Result<T>

Success.

Examples

Result<int> result = 42; // Implicit conversion to Success(42)