Skip to content

Result<T> struct

Namespace: Vitreous
Assembly: Vitreous.dll

Represents the outcome of a command or query that returns a value of type T. This is a discriminated union: either a success value or an Error.

public readonly struct Result<T> : IEquatable<Result<T>>

T

The success value type.

IEquatable<Result<T>>

object.Equals(object?), object.Equals(object?, object?), object.GetHashCode(), object.GetType(), object.ReferenceEquals(object?, object?), object.ToString()

The failure error. For an uninitialized (default) result this is Uninitialized.

public Error Error { get; }

Error

InvalidOperationException

Thrown when the result is a success.

Returns true when the result represents a failure.

public bool IsFailure { get; }

bool

Returns true when the result represents a success.

public bool IsSuccess { get; }

bool

The success value.

public T Value { get; }

T

InvalidOperationException

Thrown when the result is a failure.

Chains a fallible projection using bind. Returns the original failure unchanged if already failed.

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

bind Func<T, Result<TOut>>

Result<TOut>

TOut

BindAsync<TOut>(Func<T, ValueTask<Result<TOut>>>)

Section titled “ BindAsync<TOut>(Func<T, ValueTask<Result<TOut>>>)”

Async variant of Bind<TOut>.

public ValueTask<Result<TOut>> BindAsync<TOut>(Func<T, ValueTask<Result<TOut>>> bind)

bind Func<T, ValueTask<Result<TOut>>>

ValueTask<Result<TOut>>

TOut

Indicates whether the current object is equal to another object of the same type.

public bool Equals(Result<T> other)

other Result<T>

An object to compare with this object.

bool

true if the current object is equal to the other parameter; otherwise, false.

Indicates whether this instance and a specified object are equal.

public override bool Equals(object? obj)

obj object?

The object to compare with the current instance.

bool

true if obj and this instance are the same type and represent the same value; otherwise, false.

Creates a failed result wrapping error.

public static Result<T> Fail(Error error)

error Error

Result<T>

ArgumentNullException

Thrown when error is null.

Returns the hash code for this instance.

public override int GetHashCode()

int

A 32-bit signed integer that is the hash code for this instance.

Projects the success value using map. Returns the original failure unchanged.

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

map Func<T, TOut>

Result<TOut>

TOut

Async variant of Map<TOut>.

public ValueTask<Result<TOut>> MapAsync<TOut>(Func<T, ValueTask<TOut>> map)

map Func<T, ValueTask<TOut>>

ValueTask<Result<TOut>>

TOut

Match<TOut>(Func<T, TOut>, Func<Error, TOut>)

Section titled “ Match<TOut>(Func<T, TOut>, Func<Error, TOut>)”

Executes onSuccess if the result is successful, otherwise executes onFailure.

public TOut Match<TOut>(Func<T, TOut> onSuccess, Func<Error, TOut> onFailure)

onSuccess Func<T, TOut>

onFailure Func<Error, TOut>

TOut

TOut

MatchAsync<TOut>(Func<T, ValueTask<TOut>>, Func<Error, ValueTask<TOut>>)

Section titled “ MatchAsync<TOut>(Func<T, ValueTask<TOut>>, Func<Error, ValueTask<TOut>>)”

Async variant of Match<TOut>.

public ValueTask<TOut> MatchAsync<TOut>(Func<T, ValueTask<TOut>> onSuccess, Func<Error, ValueTask<TOut>> onFailure)

onSuccess Func<T, ValueTask<TOut>>

onFailure Func<Error, ValueTask<TOut>>

ValueTask<TOut>

TOut

Creates a successful result wrapping value.

public static Result<T> Ok(T value)

value T

Result<T>

ArgumentNullException

Thrown when value is null. A successful Result cannot hold a null value.

Returns the fully qualified type name of this instance.

public override string ToString()

string

The fully qualified type name.

Returns true when both results are equal.

public static bool operator ==(Result<T> left, Result<T> right)

left Result<T>

right Result<T>

bool

Implicitly wraps a value as a successful result.

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

value T

Result<T>

Implicitly wraps an error as a failed result.

public static implicit operator Result<T>(Error error)

error Error

Result<T>

Returns true when the results differ.

public static bool operator !=(Result<T> left, Result<T> right)

left Result<T>

right Result<T>

bool