Struct Result<T>
- Namespace
- Rascal
- Assembly
- Rascal.dll
A type which contains either an ok value or an error.
public readonly struct Result<T> : IEquatable<T>, IEquatable<Result<T>>
Type Parameters
T
The type of an ok value.
- Implements
-
IEquatable<T>IEquatable<Result<T>>
- Inherited Members
Constructors
Result(Error)
Creates a new result with an error.
public Result(Error error)
Parameters
error
ErrorThe error of the result.
Result(T)
Creates a new result with an ok value.
public Result(T value)
Parameters
value
TThe ok value.
Properties
IsError
Whether the result is an error.
public bool IsError { get; }
Property Value
- bool
- A type which contains either an ok value or an error.
Remarks
This is always the inverse of IsOk but is more specific about intent.
IsOk
Whether the result is ok.
public bool IsOk { get; }
Property Value
- bool
- A type which contains either an ok value or an error.
Methods
AsRef()
Gets an immutable reference to the ok value of the result.
[Pure]
public ref readonly T? AsRef()
Returns
- T
An immutable reference to the ok value. The value of the reference might be default if the result is an error.
Combine<TOther>(Result<TOther>)
Combines the result with another result.
[Pure]
public Result<(T first, TOther second)> Combine<TOther>(Result<TOther> other)
Parameters
other
Result<TOther>The result to combine the current result with.
Returns
- Result<(T first, TOther second)>
A result containing a tuple of the ok value of the current result and the ok value of
other
. If either of the results are errors, returns a result containing that error, or both errors if both results are errors.
Type Parameters
TOther
The type of the ok value in the other result.
Const<TNew>(TNew)
Replaces the ok value of the result with a new value, or does nothing if the result is an error.
[Pure]
public Result<TNew> Const<TNew>(TNew newValue)
Parameters
newValue
TNewThe new value to replace the ok value of the result with.
Returns
- Result<TNew>
A result which is either
newValue
if the original result was ok, or an error if the original result was an error.
Type Parameters
TNew
The type of the new value.
Equals(Result<T>)
Checks whether the result is equal to another result. Results are equal if both results are ok and the ok values are equal, or if both results are errors.
[Pure]
public bool Equals(Result<T> other)
Parameters
other
Result<T>The result to check for equality with the current result.
Returns
- bool
- A type which contains either an ok value or an error.
Equals(Result<T>, IEqualityComparer<T>)
Checks whether the result is equal to another result using a specified equality comparer. Results are equal if both results are ok and the ok values are equal, or if both results are errors.
[Pure]
public bool Equals(Result<T> other, IEqualityComparer<T> valueComparer)
Parameters
other
Result<T>The result to check for equality with the current result.
valueComparer
IEqualityComparer<T>The equality comparer to use for comparing values.
Returns
- bool
- A type which contains either an ok value or an error.
Equals(object?)
[Pure]
public override bool Equals(object? other)
Parameters
other
object- A type which contains either an ok value or an error.
Returns
- bool
- A type which contains either an ok value or an error.
Equals(T?)
Checks whether the result is ok and the ok value is equal to another value.
[Pure]
public bool Equals(T? other)
Parameters
other
TThe value to check for equality with the ok value of the result.
Returns
- bool
- A type which contains either an ok value or an error.
Equals(T?, IEqualityComparer<T>)
Checks whether the result is ok and the ok value is equal to another value using a specified equality comparer.
[Pure]
public bool Equals(T? other, IEqualityComparer<T> valueComparer)
Parameters
other
TThe value to check for equality with the ok value of the result.
valueComparer
IEqualityComparer<T>The equality comparer to use for comparing values.
Returns
- bool
- A type which contains either an ok value or an error.
Expect(string)
Expects the result to be ok, throwing an UnwrapException with a specified message if the result is an error.
[Pure]
public T Expect(string error)
Parameters
error
stringThe message to construct the UnwrapException to throw using if the result is an error.
Returns
- T
The ok value of the result.
Remarks
This API is unsafe in the sense that it might intentionally throw an exception. Please only use this API if the caller knows without a reasonable shadow of a doubt that this operation is safe, or that an exception is acceptable to be thrown.
Exceptions
- UnwrapException
The result is an error.
GetHashCode()
[Pure]
public override int GetHashCode()
Returns
- int
- A type which contains either an ok value or an error.
GetValueOr(Func<Error, T>)
Gets the ok value of the result, or a default value if the result is an error.
[Pure]
public T GetValueOr(Func<Error, T> getValue)
Parameters
Returns
- T
GetValueOr(Func<T>)
Gets the ok value of the result, or a default value if the result is an error.
[Pure]
public T GetValueOr(Func<T> getDefault)
Parameters
getDefault
Func<T>A function to get a default value to return if the result is an error.
Returns
- T
- A type which contains either an ok value or an error.
GetValueOr(T)
Gets the ok value of the result, or a default value if the result is an error.
[Pure]
public T GetValueOr(T @default)
Parameters
default
TThe default value to return if the result is an error.
Returns
- T
- A type which contains either an ok value or an error.
GetValueOrDefault()
Gets the ok value of the result, or default if the result is an error.
[Pure]
public T? GetValueOrDefault()
Returns
- T
- A type which contains either an ok value or an error.
MapAsync<TNew>(Func<T, Task<TNew>>)
Maps the ok value of the result using an asynchronous mapping function, or does nothing if the result is an error.
[Pure]
public Task<Result<TNew>> MapAsync<TNew>(Func<T, Task<TNew>> mapping)
Parameters
Returns
- Task<Result<TNew>>
A new result containing either the mapped ok value or the error of the original result.
Type Parameters
TNew
The type of the new value.
MapError(Func<Error, Error>)
Maps the error in the result if it is an error and returns a new result containing the mapped error. Otherwise, returns the current result.
[Pure]
public Result<T> MapError(Func<Error, Error> mapping)
Parameters
Returns
- Result<T>
A result which contains either the mapped error or the ok value of the original result.
Map<TNew>(Func<T, TNew>)
Maps the ok value of the result using a mapping function, or does nothing if the result is an error.
[Pure]
public Result<TNew> Map<TNew>(Func<T, TNew> mapping)
Parameters
mapping
Func<T, TNew>The function used to map the ok value.
Returns
- Result<TNew>
A new result containing either the mapped ok value or the error of the original result.
Type Parameters
TNew
The type of the new value.
MatchAsync<TResult>(Func<T, Task<TResult>>, Func<Error, Task<TResult>>)
Asynchronously matches over the ok value or error of the result and returns another value.
Can be conceptualized as an exhaustive switch
expression matching
all possible values of the type.
[Pure]
public Task<TResult> MatchAsync<TResult>(Func<T, Task<TResult>> ifOk, Func<Error, Task<TResult>> ifError)
Parameters
ifOk
Func<T, Task<TResult>>The function to apply to the ok value of the result if the result is ok.
ifError
Func<Error, Task<TResult>>The function to apply to the result's error if the result is an error.
Returns
- Task<TResult>
The result of applying either
ifOk
orifError
on the ok value or error of the result.
Type Parameters
TResult
The type to return from the match.
Match<TResult>(Func<T, TResult>, Func<Error, TResult>)
Matches over the ok value or error of the result and returns another value.
Can be conceptualized as an exhaustive switch
expression matching
all possible values of the type.
[Pure]
public TResult Match<TResult>(Func<T, TResult> ifOk, Func<Error, TResult> ifError)
Parameters
ifOk
Func<T, TResult>The function to apply to the ok value of the result if the result is ok.
ifError
Func<Error, TResult>The function to apply to the result's error if the result is an error.
Returns
- TResult
The result of applying either
ifOk
orifError
on the ok value or error of the result.
Type Parameters
TResult
The type to return from the match.
SelectMany<TNew>(Func<T, Result<TNew>>)
Maps the ok value of the result to a new result using a mapping function, or does nothing if the result is an error.
[Pure]
public Result<TNew> SelectMany<TNew>(Func<T, Result<TNew>> mapping)
Parameters
Returns
- Result<TNew>
A result which is either the mapped result or a new result containing the error of the original result.
Type Parameters
TNew
The type of the new value.
SelectMany<TOther, TNew>(Func<T, Result<TOther>>, Func<T, TOther, TNew>)
Maps the ok value of the result to an intermediate result using a mapping function, then applies another mapping function onto the ok values of the original and intermediate results, or does nothing if either of the results is an error.
[Pure]
public Result<TNew> SelectMany<TOther, TNew>(Func<T, Result<TOther>> other, Func<T, TOther, TNew> mapping)
Parameters
other
Func<T, Result<TOther>>The function used to map the ok value to an intermediate result.
mapping
Func<T, TOther, TNew>The function used to map the ok value and ok value of the intermediate result to a new result.
Returns
- Result<TNew>
A result constructed by applying
mapping
onto the ok value contained in the result returned byother
as well as the ok value in the original result, or a new result containing the error of the original or intermediate results.
Type Parameters
TOther
The type of the intermediate value.
TNew
The type of the new value.
Remarks
The expression r.SelectMany(x => f(x), (x, y) => g(x, y))
can be written as
r.Bind(x => f(x).Map(y => g(x, y)))
.
Select<TNew>(Func<T, TNew>)
Maps the ok value of the result using a mapping function, or does nothing if the result is an error.
[Pure]
public Result<TNew> Select<TNew>(Func<T, TNew> mapping)
Parameters
mapping
Func<T, TNew>The function used to map the ok value.
Returns
- Result<TNew>
A new result containing either the mapped ok value or the error of the original result.
Type Parameters
TNew
The type of the new value.
Switch(Action<T>, Action<Error>)
Matches over the ok value or error of the result and invokes an effect-ful action onto the ok value or error.
Can be conceptualized as an exhaustive switch
statement matching all possible values of the type.
public void Switch(Action<T> ifOk, Action<Error> ifError)
Parameters
ifOk
Action<T>The function to call with the ok value of the result if the result is ok.
ifError
Action<Error>The function to call with the result's error if the result is an error.
SwitchAsync(Func<T, Task>, Func<Error, Task>)
Asynchronously matches over the ok value or error of the result
and invokes an effect-ful action onto the ok value or error.
Can be conceptualized as an exhaustive switch
statement matching all possible values of the type.
public Task SwitchAsync(Func<T, Task> ifOk, Func<Error, Task> ifError)
Parameters
ifOk
Func<T, Task>The function to call with the ok value of the result if the result is ok.
ifError
Func<Error, Task>The function to call with the result's error if the result is an error.
Returns
- Task
- A type which contains either an ok value or an error.
ThenAsync<TNew>(Func<T, Task<Result<TNew>>>)
Maps the ok value of the result to a new result using an asynchronous mapping function, or does nothing if the result is an error.
[Pure]
public Task<Result<TNew>> ThenAsync<TNew>(Func<T, Task<Result<TNew>>> mapping)
Parameters
Returns
- Task<Result<TNew>>
A result which is either the mapped result or a new result containing the error of the original result.
Type Parameters
TNew
The type of the new value.
ThenTryAsync<TNew>(Func<T, Task<Result<TNew>>>)
Tries to map the ok value of the result to a new result using an asynchronous mapping function, or does nothing if the result is an error. If the mapping function throws an exception, the exception will be returned wrapped in an ExceptionError.
[Pure]
public Task<Result<TNew>> ThenTryAsync<TNew>(Func<T, Task<Result<TNew>>> mapping)
Parameters
Returns
- Task<Result<TNew>>
A result which is either the mapped result, the exception thrown by
mapping
wrapped in an ExceptionError, or a new result containing the error of the original result.
Type Parameters
TNew
The type of the new value.
ThenTry<TNew>(Func<T, Result<TNew>>)
Tries to map the ok value of the result to a new result using a mapping function, or does nothing if the result is an error. If the mapping function throws an exception, the exception will be returned wrapped in an ExceptionError.
[Pure]
public Result<TNew> ThenTry<TNew>(Func<T, Result<TNew>> mapping)
Parameters
Returns
- Result<TNew>
A result which is either the mapped result, the exception thrown by
mapping
wrapped in an ExceptionError, or a new result containing the error of the original result.
Type Parameters
TNew
The type of the new value.
Then<TNew>(Func<T, Result<TNew>>)
Maps the ok value of the result to a new result using a mapping function, or does nothing if the result is an error.
[Pure]
public Result<TNew> Then<TNew>(Func<T, Result<TNew>> mapping)
Parameters
Returns
- Result<TNew>
A result which is either the mapped result or a new result containing the error of the original result.
Type Parameters
TNew
The type of the new value.
ToEnumerable()
Creates a IEnumerable<T> from the result.
[Pure]
public IEnumerable<T> ToEnumerable()
Returns
- IEnumerable<T>
A IEnumerable<T> containing either only the ok value of the result, or no values at all if the result is an error.
ToString()
Gets a string representation of the result.
[Pure]
public override string ToString()
Returns
- string
- A type which contains either an ok value or an error.
To<TOther>(Error?)
Tries to convert the ok value of the result to another type.
[Pure]
public Result<TOther> To<TOther>(Error? error = null)
Parameters
error
ErrorThe error to return if the ok value cannot be converted to
TOther
.
Returns
- Result<TOther>
A result which contains the ok value converted to
TOther
,error
if the ok value cannot be converted toTOther
, or the error of the result if it is an error.
Type Parameters
TOther
The type to which to try convert the ok value.
TryGetError(out Error)
Tries to get an error from the result.
[Pure]
public bool TryGetError(out Error error)
Parameters
error
ErrorThe error of the result.
Returns
- bool
Whether the result is an error.
TryGetError(out Error, out T)
Tries to get an error from the result.
[Pure]
public bool TryGetError(out Error error, out T value)
Parameters
error
ErrorThe error of the result.
value
TThe ok value of the result.
Returns
- bool
Whether the result is an error.
TryGetValue(out T)
Tries to get the ok value from the result.
[Pure]
public bool TryGetValue(out T value)
Parameters
value
TThe ok value of the result.
Returns
- bool
Whether the result is ok.
TryGetValue(out T, out Error)
Tries to get the ok value from the result.
[Pure]
public bool TryGetValue(out T value, out Error error)
Parameters
value
TThe ok value of the result.
error
ErrorThe error of the result.
Returns
- bool
Whether the result is ok.
TryMapAsync<TNew>(Func<T, Task<TNew>>)
Tries to map the ok value of the result using an asynchronous mapping function, or does nothing if the result is an error. If the mapping function throws an exception, the exception will be returned wrapped in an ExceptionError.
[Pure]
public Task<Result<TNew>> TryMapAsync<TNew>(Func<T, Task<TNew>> mapping)
Parameters
Returns
- Task<Result<TNew>>
A new result containing either the mapped ok value, the exception thrown by
mapping
wrapped in an ExceptionError, or the error of the original result.
Type Parameters
TNew
The type of the new value.
TryMap<TNew>(Func<T, TNew>)
Tries to map the ok value of the result using a mapping function, or does nothing if the result is an error. If the mapping function throws an exception, the exception will be returned wrapped in an ExceptionError.
[Pure]
public Result<TNew> TryMap<TNew>(Func<T, TNew> mapping)
Parameters
mapping
Func<T, TNew>The function used to map the ok value.
Returns
- Result<TNew>
A new result containing either the mapped value, the exception thrown by
mapping
wrapped in an ExceptionError, or the error of the original result.
Type Parameters
TNew
The type of the new value.
Unwrap()
Unwraps the ok value of the result. Throws an UnwrapException if the result is an error.
[Pure]
public T Unwrap()
Returns
- T
The ok value of the result.
Remarks
This API is unsafe in the sense that it might intentionally throw an exception. Please only use this API if the caller knows without a reasonable shadow of a doubt that this operation is safe, or that an exception is acceptable to be thrown.
Exceptions
- UnwrapException
The result is not ok.
Validate(Func<T, bool>, Func<T, Error>?)
Checks whether the ok value in the result matches a predicate, and if not replaces the value with an error.
[Pure]
public Result<T> Validate(Func<T, bool> predicate, Func<T, Error>? getError = null)
Parameters
predicate
Func<T, bool>The predicate to match the ok value against.
getError
Func<T, Error>A function to produce an error if the ok value doesn't match the predicate.
Returns
- Result<T>
A result which contains the ok value of the original result if said value matches
predicate
, otherwise an error produced bygetError
. If the original result is an error, that error will be returned.
Where(Func<T, bool>)
Checks whether the ok value of the result matches a predicate, and if not replaces the value with an error.
[Pure]
public Result<T> Where(Func<T, bool> predicate)
Parameters
Returns
- Result<T>
A result which contains the ok value of the original result if said value matches
predicate
, otherwise an error. If the original result is an error, that error will be returned.
Operators
operator ==(Result<T>, Result<T>)
Checks whether two results are equal. Results are equal if both results are ok and the ok values are equal, or if both results are errors.
[Pure]
public static bool operator ==(Result<T> a, Result<T> b)
Parameters
Returns
- bool
- A type which contains either an ok value or an error.
operator ==(Result<T>, T?)
Checks whether a result is ok and the ok value is equal to another value.
[Pure]
public static bool operator ==(Result<T> a, T? b)
Parameters
a
Result<T>The result to compare.
b
TThe value to check for equality with the ok value in the result.
Returns
- bool
- A type which contains either an ok value or an error.
explicit operator T(Result<T>)
Unwraps the ok value of the result. Throws an UnwrapException if the result is an error.
[Pure]
public static explicit operator T(Result<T> result)
Parameters
result
Result<T>- A type which contains either an ok value or an error.
Returns
- T
The ok value of the result.
Remarks
This API is unsafe in the sense that it might intentionally throw an exception. Please only use this API if the caller knows without a reasonable shadow of a doubt that this operation is safe, or that an exception is acceptable to be thrown.
Exceptions
- UnwrapException
The result is not ok.
implicit operator Result<T>(Error)
Implicitly constructs a result from an error.
[Pure]
public static implicit operator Result<T>(Error error)
Parameters
error
ErrorThe error to construct the result from.
Returns
- Result<T>
- A type which contains either an ok value or an error.
implicit operator Result<T>(T)
Implicitly constructs a result from an ok value.
[Pure]
public static implicit operator Result<T>(T value)
Parameters
value
TThe value to construct the result from.
Returns
- Result<T>
- A type which contains either an ok value or an error.
operator !=(Result<T>, Result<T>)
Checks whether two results are not equal. Results are equal if both results are ok and the ok values are equal, or if both results are errors.
[Pure]
public static bool operator !=(Result<T> a, Result<T> b)
Parameters
Returns
- bool
- A type which contains either an ok value or an error.
operator !=(Result<T>, T?)
Checks whether a result either does not have a value, or the value is not equal to another value.
[Pure]
public static bool operator !=(Result<T> a, T? b)
Parameters
a
Result<T>The result to compare.
b
TThe value to check for inequality with the ok value in the result.
Returns
- bool
- A type which contains either an ok value or an error.