Struct Result<T>
Represents the result of an operation that can succeed or fail.
Inherited Members
Namespace: MediatorForge.Results
Assembly: MediatorForge.dll
Syntax
public readonly struct Result<T>
Type Parameters
Name | Description |
---|---|
T | The type of the value. |
Constructors
| Improve this Doc View SourceResult(T)
Initializes a new instance of the Result<T> struct with a successful value.
Declaration
public Result(T value)
Parameters
Type | Name | Description |
---|---|---|
T | value | The value of the successful result. |
Result(Exception)
Initializes a new instance of the Result<T> struct with a failure exception.
Declaration
public Result(Exception error)
Parameters
Type | Name | Description |
---|---|---|
Exception | error | The exception of the failed result. |
Properties
| Improve this Doc View SourceException
Gets the exception of the result if it is a failure.
Declaration
public readonly Exception? Exception { get; }
Property Value
Type | Description |
---|---|
System.Nullable<Exception> |
IsSuccess
Gets a value indicating whether the result is successful.
Declaration
public readonly bool IsSuccess { get; }
Property Value
Type | Description |
---|---|
System.Boolean |
Value
Gets the value of the result if it is successful.
Declaration
public readonly T Value { get; }
Property Value
Type | Description |
---|---|
T |
Methods
| Improve this Doc View SourceCompareTo(Result<T>)
Compares this instance with another Result<T> and returns an integer that indicates whether this instance precedes, follows, or occurs in the same position in the sort order as the other Result<T>.
Declaration
public readonly int CompareTo(Result<T> other)
Parameters
Type | Name | Description |
---|---|---|
Result<T> | other | A Result<T> to compare with this instance. |
Returns
Type | Description |
---|---|
System.Int32 | A value that indicates the relative order of the objects being compared. |
Equals(Result<T>)
Determines whether this instance and another specified Result<T> have the same value or exception.
Declaration
public readonly bool Equals(Result<T> other)
Parameters
Type | Name | Description |
---|---|---|
Result<T> | other | The Result<T> to compare to this instance. |
Returns
Type | Description |
---|---|
System.Boolean |
|
Equals(Object)
Determines whether the specified object is equal to this instance.
Declaration
public override readonly bool Equals(object obj)
Parameters
Type | Name | Description |
---|---|---|
System.Object | obj | The object to compare with this instance. |
Returns
Type | Description |
---|---|
System.Boolean |
|
Overrides
Fail(Exception)
Creates a failed result.
Declaration
public static Result<T> Fail(Exception error)
Parameters
Type | Name | Description |
---|---|---|
Exception | error | The exception of the failed result. |
Returns
Type | Description |
---|---|
Result<T> | A failed Result<T>. |
GetHashCode()
Returns the hash code for this instance.
Declaration
public override readonly int GetHashCode()
Returns
Type | Description |
---|---|
System.Int32 | A hash code for the current object. |
Overrides
Match(Action<T>, Action<Exception>)
Executes one of the specified functions based on whether the result is successful or failed.
Declaration
public readonly void Match(Action<T> onSuccess, Action<Exception> onFailure)
Parameters
Type | Name | Description |
---|---|---|
Action<T> | onSuccess | The function to execute if the result is successful. |
Action<Exception> | onFailure | The function to execute if the result is failed. |
Match<TResult>(Func<T, TResult>, Func<Exception, TResult>)
Executes one of the specified functions based on whether the result is successful or failed.
Declaration
public readonly TResult Match<TResult>(Func<T, TResult> onSuccess, Func<Exception, TResult> onFailure)
Parameters
Type | Name | Description |
---|---|---|
Func<T, TResult> | onSuccess | The function to execute if the result is successful. |
Func<Exception, TResult> | onFailure | The function to execute if the result is failed. |
Returns
Type | Description |
---|---|
TResult | The result of the executed function. |
Type Parameters
Name | Description |
---|---|
TResult | The type of the result produced by the functions. |
OnFailure(Action<Exception>)
Executes the specified action if the result is failed.
Declaration
public readonly void OnFailure(Action<Exception> onFailure)
Parameters
Type | Name | Description |
---|---|---|
Action<Exception> | onFailure | The action to execute if the result is failed. |
OnSuccess(Action<T>)
Executes the specified action if the result is successful.
Declaration
public readonly void OnSuccess(Action<T> onSuccess)
Parameters
Type | Name | Description |
---|---|---|
Action<T> | onSuccess | The action to execute if the result is successful. |
Succ(T)
Creates a successful result.
Declaration
public static Result<T> Succ(T value)
Parameters
Type | Name | Description |
---|---|---|
T | value | The value of the successful result. |
Returns
Type | Description |
---|---|
Result<T> | A successful Result<T>. |
Operators
| Improve this Doc View SourceEquality(Result<T>, Result<T>)
Determines whether two Result<T> instances are equal.
Declaration
public static bool operator ==(Result<T> left, Result<T> right)
Parameters
Type | Name | Description |
---|---|---|
Result<T> | left | The first Result<T> to compare. |
Result<T> | right | The second Result<T> to compare. |
Returns
Type | Description |
---|---|
System.Boolean |
|
GreaterThan(Result<T>, Result<T>)
Determines whether one Result<T> instance follows another in the sort order.
Declaration
public static bool operator>(Result<T> left, Result<T> right)
Parameters
Type | Name | Description |
---|---|---|
Result<T> | left | The first Result<T> to compare. |
Result<T> | right | The second Result<T> to compare. |
Returns
Type | Description |
---|---|
System.Boolean |
|
GreaterThanOrEqual(Result<T>, Result<T>)
Determines whether one Result<T> instance follows or is equal to another in the sort order.
Declaration
public static bool operator >=(Result<T> left, Result<T> right)
Parameters
Type | Name | Description |
---|---|---|
Result<T> | left | The first Result<T> to compare. |
Result<T> | right | The second Result<T> to compare. |
Returns
Type | Description |
---|---|
System.Boolean |
|
Implicit(T to Result<T>)
Implicitly converts a value to a successful Result<T> containing that value.
Declaration
public static implicit operator Result<T>(T value)
Parameters
Type | Name | Description |
---|---|---|
T | value | The value to convert. |
Returns
Type | Description |
---|---|
Result<T> |
Implicit(Exception to Result<T>)
Implicitly converts an exception to a failed Result<T> containing that exception.
Declaration
public static implicit operator Result<T>(Exception error)
Parameters
Type | Name | Description |
---|---|---|
Exception | error | The exception to convert. |
Returns
Type | Description |
---|---|
Result<T> |
Inequality(Result<T>, Result<T>)
Determines whether two Result<T> instances are not equal.
Declaration
public static bool operator !=(Result<T> left, Result<T> right)
Parameters
Type | Name | Description |
---|---|---|
Result<T> | left | The first Result<T> to compare. |
Result<T> | right | The second Result<T> to compare. |
Returns
Type | Description |
---|---|
System.Boolean |
|
LessThan(Result<T>, Result<T>)
Determines whether one Result<T> instance precedes another in the sort order.
Declaration
public static bool operator <(Result<T> left, Result<T> right)
Parameters
Type | Name | Description |
---|---|---|
Result<T> | left | The first Result<T> to compare. |
Result<T> | right | The second Result<T> to compare. |
Returns
Type | Description |
---|---|
System.Boolean |
|
LessThanOrEqual(Result<T>, Result<T>)
Determines whether one Result<T> instance precedes or is equal to another in the sort order.
Declaration
public static bool operator <=(Result<T> left, Result<T> right)
Parameters
Type | Name | Description |
---|---|---|
Result<T> | left | The first Result<T> to compare. |
Result<T> | right | The second Result<T> to compare. |
Returns
Type | Description |
---|---|
System.Boolean |
|