Struct Result<TValue>
- Namespace
- Badeend
- Assembly
- Badeend.Result.dll
Represents the result of a fallible operation. This type is a shorthand for: Result<TValue, Badeend.Error>.
public readonly struct Result<TValue> : IEquatable<Result<TValue>>, IComparable<Result<TValue>>, IComparable
Type Parameters
TValue
Type of the result when the operation succeeds.
- Implements
-
IEquatable<Result<TValue>>IComparable<Result<TValue>>
- Inherited Members
- Extension Methods
Remarks
A Result
can be in one of two states:
Success or
Error.
Both states have an associated payload of type TValue
or Error respectively.
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.
Note that you should generally not attempt to
derive any semantic meaning from the Error's content.
This type (or its longhand form Result<T, Badeend.Error>
)
is semantically the same as Result<T, void>
in that: all that the
domain logic should care about is whether the operation succeeded or failed.
The Error data is just a way to carry additional developer-oriented context.
A Result's default
value is equivalent to Result.Error(default!)
.
Properties
Error
Get the error value.
public ref readonly Error Error { get; }
Property Value
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>)
Compare two results.
Successful results precede Error results.
[Pure]
public int CompareTo(Result<TValue> other)
Parameters
other
Result<TValue>
Returns
- int
See IComparable<T>.CompareTo(T) for more information.
Exceptions
- ArgumentException
TValue
orTError
does not implement IComparable.
Equals(Result<TValue>)
Check for equality.
[Pure]
public bool Equals(Result<TValue> other)
Parameters
other
Result<TValue>
Returns
GetErrorOrDefault()
Attempt to get the operation's error value. Returns default when the operation succeeded.
[Pure]
public Error GetErrorOrDefault()
Returns
GetErrorOrDefault(Error)
Attempt to get the operation's error value.
Returns defaultValue
when the operation succeeded.
[Pure]
public Error GetErrorOrDefault(Error defaultValue)
Parameters
defaultValue
Error
Returns
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
defaultValue
TValue
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 Error)
Attempt to store the operation's error in error
.
Returns false when the operation succeeded.
public bool TryGetError(out Error error)
Parameters
error
Error
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
value
TValue
Returns
TryGetValue(out TValue, out Error)
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 Error error)
Parameters
value
TValueerror
Error
Returns
Operators
operator ==(Result<TValue>, Result<TValue>)
Check for equality.
[Pure]
public static bool operator ==(Result<TValue> left, Result<TValue> right)
Parameters
Returns
implicit operator Result<TValue>(Error)
Create a error result.
[Pure]
public static implicit operator Result<TValue>(Error error)
Parameters
error
Error
Returns
- Result<TValue>
implicit operator Result<TValue>(Result<TValue, Error>)
Convert from a Result with an explicit error type to a Result with an implicit error type.
[Pure]
public static implicit operator Result<TValue>(Result<TValue, Error> result)
Parameters
Returns
- Result<TValue>
implicit operator Result<TValue, Error>(Result<TValue>)
Convert from a Result with an implicit error type to a Result with an explicit error type.
[Pure]
public static implicit operator Result<TValue, Error>(Result<TValue> result)
Parameters
result
Result<TValue>
Returns
implicit operator Result<TValue>(TValue)
Create a successful result.
[Pure]
public static implicit operator Result<TValue>(TValue value)
Parameters
value
TValue
Returns
- Result<TValue>
operator !=(Result<TValue>, Result<TValue>)
Check for inequality.
[Pure]
public static bool operator !=(Result<TValue> left, Result<TValue> right)