Result object holds the result of an operation or Error
It is defined as
public readonly struct Result<TValue>
{
public TValue Value => IsSuccess ? _value! : throw new InvalidOperationException;
public Error Error => IsFailure ? _error! : throw new InvalidOperationException;
public bool IsSuccess => !IsFailure;
public bool IsFailure { get; }
public static implicit operator Result<TValue>(TValue value) => Result.Success(value);
public static implicit operator Result<TValue>(Error error) => Result.Failure<TValue>(error);
}
Maybe object holds a value or nothing. It is defined as
public readonly struct Maybe<T> :
IEquatable<T>,
IEquatable<Maybe<T>>
where T : notnull
{
public T Value;
public bool HasValue;
public bool HasNoValue;
}
Bind calls the given function if the result is in success state and return the new result.
Tap calls the given function if the result is in success state and returns the same result.
Compensate for failed result by calling the given function.
Ensure calls the given function if the result is in success state. If the function returns false, the attached error is returned.
Map calls the given function if the result is in success state.
The return value is wrapped in Result
as success.
Combine combines multiple Result
objects. It will return a Result
with all the errors if any of the Result
objects have failed.
If all the errors are of type ValidationError
, then it will return a ValidationError
with all the errors.
Otherwise it will return an AggregatedError
Parallel runs multiple tasks in parallel and returns multiple tasks. AwaitAsync
will await all the task and return the combined result.
Finally unwraps the Result
and returns the success value or the error.
Maybe states if it contains a value or not. It has the following methods:
- HasValue - returns true if it has a value.
- HasNoValue - returns true if it does not have a value.
- Value - returns the value if it has a value. Otherwise
InvalidOperationException