Struct Result<TValue, TError>
- Namespace
- Badeend
- Assembly
- Badeend.Result.dll
Represents the result of a fallible operation.
A Result can be in one of two states:
Success or
Error.
Both states have an associated payload of type TValue
or TError respectively.
[SuppressMessage("Naming", "CA1708:Identifiers should differ by more than case", Justification = "Internal")]
[SuppressMessage("Design", "CA1036:Override methods on comparable types", Justification = "Result is only comparable if TValue and TError are, which we can't know at compile time. Don't want to promote the comparable stuff too much.")]
public readonly struct Result<TValue, TError> : IEquatable<Result<TValue, TError>>, IComparable<Result<TValue, TError>>, IComparable
Type Parameters
TValueType of the result when the operation succeeds.
TErrorType of the result when the operation fails.
- Implements
-
IEquatable<Result<TValue, TError>>IComparable<Result<TValue, TError>>
- Inherited Members
Remarks
Because of the implicit conversion operators you typically don't have to manually construct Results. If you do want or need to, you can use Result.Success() or Result.Error() instead.
You can examine a result like this:
_ = myResult.State switch
{
ResultState.Success => $"Something successful: {myResult.Value}",
ResultState.Error => $"Something failed: {myResult.Error}",
};
Or alternatively using IsSuccess, IsError, TryGetValue, TryGetError, GetValueOrDefault or GetErrorOrDefault.
A Result's default value is equivalent to Result.Error(default!).
Properties
Error
Get the error value.
public ref readonly TError Error { get; }
Property Value
- TError
Exceptions
- InvalidOperationException
The operation did not fail.
IsError
Check whether the operation failed.
[Pure]
public bool IsError { get; }
Property Value
IsSuccess
Check whether the operation succeeded.
[Pure]
public bool IsSuccess { get; }
Property Value
State
[Pure]
public ResultState State { get; }
Property Value
Value
Get the success value.
public ref readonly TValue Value { get; }
Property Value
- TValue
Exceptions
- InvalidOperationException
The operation was not successful.
Methods
CompareTo(Result<TValue, TError>)
Compare two results.
Successful results precede Error results.
[Pure]
public int CompareTo(Result<TValue, TError> other)
Parameters
otherResult<TValue, TError>
Returns
- int
See IComparable<T>.CompareTo(T) for more information.
Exceptions
- ArgumentException
TValueorTErrordoes not implement IComparable.
Equals(Result<TValue, TError>)
Check for equality.
[Pure]
public bool Equals(Result<TValue, TError> other)
Parameters
otherResult<TValue, TError>
Returns
GetErrorOrDefault()
Attempt to get the operation's error value. Returns default when the operation succeeded.
[Pure]
public TError? GetErrorOrDefault()
Returns
- TError
GetErrorOrDefault(TError)
Attempt to get the operation's error value.
Returns defaultValue when the operation succeeded.
[Pure]
public TError GetErrorOrDefault(TError defaultValue)
Parameters
defaultValueTError
Returns
- TError
GetHashCode()
[Pure]
public override int GetHashCode()
Returns
GetValueOrDefault()
Attempt to get the operation's success value. Returns default when the operation failed.
[Pure]
public TValue? GetValueOrDefault()
Returns
- TValue
GetValueOrDefault(TValue)
Attempt to get the operation's success value.
Returns defaultValue when the operation failed.
[Pure]
public TValue GetValueOrDefault(TValue defaultValue)
Parameters
defaultValueTValue
Returns
- TValue
ToString()
Get a string representation of the result for debugging purposes. The format is not stable and may change without prior notice.
[Pure]
public override string ToString()
Returns
TryGetError(out TError)
Attempt to store the operation's error in error.
Returns false when the operation succeeded.
public bool TryGetError(out TError error)
Parameters
errorTError
Returns
TryGetValue(out TValue)
Attempt to store the operation's success value in value.
Returns false when the operation failed.
public bool TryGetValue(out TValue value)
Parameters
valueTValue
Returns
TryGetValue(out TValue, out TError)
Attempt to store the operation's success value in value.
If the operation failed, this method returns false
and the error is stored in error.
public bool TryGetValue(out TValue value, out TError error)
Parameters
valueTValueerrorTError
Returns
Operators
operator ==(Result<TValue, TError>, Result<TValue, TError>)
Check for equality.
[Pure]
public static bool operator ==(Result<TValue, TError> left, Result<TValue, TError> right)
Parameters
Returns
implicit operator Result<TValue, TError>(TValue)
Create a successful result.
[Pure]
public static implicit operator Result<TValue, TError>(TValue value)
Parameters
valueTValue
Returns
- Result<TValue, TError>
implicit operator Result<TValue, TError>(TError)
Create a error result.
[Pure]
public static implicit operator Result<TValue, TError>(TError error)
Parameters
errorTError
Returns
- Result<TValue, TError>
operator !=(Result<TValue, TError>, Result<TValue, TError>)
Check for inequality.
[Pure]
public static bool operator !=(Result<TValue, TError> left, Result<TValue, TError> right)