Table of Contents

Result Struct

Definition

Namespace
Bodu.Functional
Assembly
Bodu.Core.dll
Package
Bodu.Core 1.0.1
Source
Result.Factories.cs

Represents the outcome of an operation that produces no value: either Success, or Failure(error) carrying a ResultError.

public readonly struct Result : IEquatable<Result>
Implements
Inherited Members
Extension Methods

Remarks

Result makes failure explicit in a signature without resorting to exceptions for expected error paths. Collapse to a final value with Match<TResult>(Func<TResult>, Func<ResultError, TResult>), or branch with Match(Action, Action<ResultError>). The type also hosts the factory surface for the whole family, including the generic Result<T> factories Success<T>(T) and Failure<T>(ResultError).

default(Result) equals a failure carrying an empty ResultError - a result that was never assigned behaves exactly like an explicit failure, so the type is total and safe to use as a field or array element without initialization. Error on default(Result) is valid and returns the empty error.

Properties

Error

Gets the error carried by a failed result.

public ResultError Error { get; }

Property Value

ResultError

The ResultError describing the failure.

Remarks

Prefer TryGetError(out ResultError) or Match<TResult>(Func<TResult>, Func<ResultError, TResult>) over direct access when success is an expected state; Error is intended for call sites that have already established IsFailure. On default(Result) it is valid and returns the empty error.

Exceptions

InvalidOperationException

The result does not represent a failure.

IsFailure

Gets a value indicating whether this result represents failure.

public bool IsFailure { get; }

Property Value

bool

true if the operation failed; otherwise, false.

IsSuccess

Gets a value indicating whether this result represents success.

public bool IsSuccess { get; }

Property Value

bool

true if the operation succeeded; otherwise, false.

Methods

Equals(Result)

Determines whether the specified result is equal to the current instance.

public bool Equals(Result other)

Parameters

other Result

The result to compare with this instance.

Returns

bool

true if both are successes, or both are failures whose errors compare equal using Equals(ResultError); otherwise, false.

Equals(object?)

Determines whether the specified object is equal to the current result.

public override bool Equals(object? obj)

Parameters

obj object

The object to compare. Only another Result can be equal; all other types, including null, return false.

Returns

bool

true if obj is a Result equal to this instance; otherwise, false.

Failure(ResultError)

Creates a result representing failure with the specified error.

public static Result Failure(ResultError error)

Parameters

error ResultError

The error describing the failure.

Returns

Result

A Result whose IsFailure is true.

Failure<T>(ResultError)

Creates a failed result of the specified value type carrying the specified error.

public static Result<T> Failure<T>(ResultError error)

Parameters

error ResultError

The error describing the failure.

Returns

Result<T>

A Result<T> whose IsFailure is true.

Type Parameters

T

The value type of the result.

GetHashCode()

Returns a hash code for the current instance consistent with Equals(Result).

public override int GetHashCode()

Returns

int

1 for success; otherwise the error's hash code.

Match(Action, Action<ResultError>)

Invokes the matching action for the state of the result.

public void Match(Action onSuccess, Action<ResultError> onFailure)

Parameters

onSuccess Action

The action invoked when the result represents success.

onFailure Action<ResultError>

The action invoked with the error when the result represents failure.

Exceptions

ArgumentNullException

onSuccess or onFailure is null.

Match<TResult>(Func<TResult>, Func<ResultError, TResult>)

Collapses the result to a single value by invoking the matching branch.

public TResult Match<TResult>(Func<TResult> onSuccess, Func<ResultError, TResult> onFailure)

Parameters

onSuccess Func<TResult>

The factory invoked when the result represents success.

onFailure Func<ResultError, TResult>

The projection invoked with the error when the result represents failure.

Returns

TResult

The value produced by the invoked branch.

Type Parameters

TResult

The type produced by both branches.

Examples

var label = result.Match(() => "ok", error => $"failed: {error}");

Exceptions

ArgumentNullException

onSuccess or onFailure is null.

Success()

Creates a result representing success.

public static Result Success()

Returns

Result

A Result whose IsSuccess is true.

Examples

var ok = Result.Success();                                    // Success
var failed = Result.Failure(ResultError.FromMessage("boom")); // Failure(boom)

Success<T>(T)

Creates a successful result carrying the specified non-null value.

public static Result<T> Success<T>(T value)

Parameters

value T

The value to wrap. Must not be null.

Returns

Result<T>

A Result<T> whose IsSuccess is true.

Type Parameters

T

The type of the value.

Remarks

This is a strict lift: a null argument is rejected because a successful result must always carry a value, mirroring Some(T).

Exceptions

ArgumentNullException

value is null.

ToString()

Returns a string representation of the result.

public override string ToString()

Returns

string

"Success" for success; otherwise "Failure(error)".

TryGetError(out ResultError)

Attempts to retrieve the error carried by a failed result.

public bool TryGetError(out ResultError error)

Parameters

error ResultError

When this method returns, the error if the result represents failure; otherwise the default error.

Returns

bool

true if the result represents failure; otherwise, false.

Operators

operator ==(Result, Result)

Determines whether two results are equal.

public static bool operator ==(Result left, Result right)

Parameters

left Result

The first result to compare.

right Result

The second result to compare.

Returns

bool

true if both are successes, or both are failures carrying equal errors; otherwise, false.

operator !=(Result, Result)

Determines whether two results are not equal.

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

Parameters

left Result

The first result to compare.

right Result

The second result to compare.

Returns

bool

true if the results differ; otherwise, false.

Applies to

ProductVersions
.NET8, 10