Class ResultExtensions
- Namespace
- Rascal
- Assembly
- Rascal.dll
Extensions for or relating to Result<T>.
public static class ResultExtensions
- Inheritance
-
ResultExtensions
- Inherited Members
Methods
CatchCancellation<T>(Task<T>, Func<Error>?)
Catches the cancellation of a task and wraps the returned value or thrown exception in a result.
public static Task<Result<T>> CatchCancellation<T>(this Task<T> task, Func<Error>? error = null)
Parameters
task
Task<T>The task to catch.
error
Func<Error>A function which produces an error in case of a cancellation.
Returns
- Task<Result<T>>
A result which contains either the value returned by awaiting
task
, or an error in case the task is cancelled.
Type Parameters
T
The type returned by the task.
GetValueOrNull<T>(Result<T>)
Gets the ok value of the result, or null if the result is an error.
[Pure]
public static T? GetValueOrNull<T>(this Result<T> result) where T : struct
Parameters
result
Result<T>The result to get the ok value of.
Returns
- T?
The ok value of
result
, or null ifresult
is an error.
Type Parameters
T
The type of the ok value of the result.
Remarks
This method differs from GetValueOrDefault() in that
it specifically targets value types and returns null as opposed
to the default value for the type in case the result is an error.
Can also be understood as mapping the value to T?
and calling
GetValueOrDefault() on the returned result.
Index<T>(IReadOnlyList<T>, int, Error?)
Gets a result containing the element at the specified index in the list, or an error if the index is out of range of the list.
public static Result<T> Index<T>(this IReadOnlyList<T> list, int index, Error? error = null)
Parameters
list
IReadOnlyList<T>The list to index.
index
intThe zero-based index of the element to get.
error
ErrorThe error to return if the index is out of range.
Returns
- Result<T>
A result containing the value at
index
in the list, or an error if the index is out of range of the list.
Type Parameters
T
The type of the elements in the list.
NotNull<T>(T?, Error?)
Turns a nullable value into a result containing a non-null ok value or an error if the value is null.
[Pure]
public static Result<T> NotNull<T>(this T? x, Error? error = null) where T : struct
Parameters
x
T?The value to turn into a result.
error
ErrorThe error to return if the value is null.
Returns
Type Parameters
T
The type of the value.
Remarks
Note that this method has two variants: one for reference types and one for nullable value types.
NotNull<T>(T?, Error?)
Turns a nullable value into a result containing a non-null ok value or an error if the value is null.
[Pure]
public static Result<T> NotNull<T>(this T? x, Error? error = null) where T : class
Parameters
x
TThe value to turn into a result.
error
ErrorThe error to return if the value is null.
Returns
Type Parameters
T
The type of the value.
Remarks
Note that this method has two variants: one for reference types and one for nullable value types.
Sequence<T>(IEnumerable<Result<T>>)
Turns a sequence of results into a single result containing the ok values in the results
only if all the results are ok.
Can also been seen as turning an IEnumerable<Result<T>>
"inside out".
public static Result<IReadOnlyList<T>> Sequence<T>(this IEnumerable<Result<T>> results)
Parameters
results
IEnumerable<Result<T>>The results to turn into a single sequence.
Returns
- Result<IReadOnlyList<T>>
A single result containing a sequence of all the ok values from the original sequence of results, or the first error encountered within the sequence.
Type Parameters
T
The type of the ok values in the results.
Remarks
This method completely enumerates the input sequence before returning and is not lazy. As a consequence of this, the sequence within the returned result is an IReadOnlyList<T>.
ToString<T>(Result<T>, IFormatProvider)
Returns a string representation of the result using a specified IFormatProvider to format an ok value.
public static string ToString<T>(this Result<T> result, IFormatProvider formatProvider) where T : IFormattable
Parameters
result
Result<T>The result to return a string representation of.
formatProvider
IFormatProviderThe provider to use to format the ok value.
Returns
- string
- Extensions for or relating to .
Type Parameters
T
The type of the ok value in the result.
ToString<T>(Result<T>, string)
Returns a string representation of the result using a specified format to format an ok value.
public static string ToString<T>(this Result<T> result, string format) where T : IFormattable
Parameters
Returns
- string
- Extensions for or relating to .
Type Parameters
T
The type of the ok value in the result.
ToString<T>(Result<T>, string?, IFormatProvider?)
Returns a string representation of the result using a specified format and IFormatProvider to format an ok value.
public static string ToString<T>(this Result<T> result, string? format, IFormatProvider? formatProvider) where T : IFormattable
Parameters
result
Result<T>The result to return a string representation of.
format
stringThe format to use.
formatProvider
IFormatProviderThe provider to use to format the ok value.
Returns
- string
- Extensions for or relating to .
Type Parameters
T
The type of the ok value in the result.
TryGetValueR<TKey, TValue>(IReadOnlyDictionary<TKey, TValue>, TKey, Error?)
Gets a result containing the value that is associated with the specified key in a dictionary, or an error if the key is not present.
public static Result<TValue> TryGetValueR<TKey, TValue>(this IReadOnlyDictionary<TKey, TValue> dict, TKey key, Error? error = null)
Parameters
dict
IReadOnlyDictionary<TKey, TValue>The dictionary to try locate the key in.
key
TKeyThe key to locate.
error
ErrorThe error to return if the key is not present.
Returns
- Result<TValue>
A result containing the value associated with
key
in the dictionary, or an error if the key is not present.
Type Parameters
TKey
The type of keys in the dictionary.
TValue
The type of values in the dictionary.
Unnest<T>(Result<Result<T>>)
Takes a result containing another result and un-nests the inner result.
[Pure]
public static Result<T> Unnest<T>(this Result<Result<T>> result)
Parameters
Returns
- Result<T>
- Extensions for or relating to .
Type Parameters
T
The type of the ok value in the result.
Remarks
This operation is the same as applying Then<TNew>(Func<T, Result<TNew>>)
with the identity function, eg. r.Then(x => x)
.