Class Result<T>
- Namespace
- CivitaiSharp.Core.Response
- Assembly
- CivitaiSharp.Core.dll
Discriminated union representing the result of an operation as either success with data or failure with error information. Use pattern matching for exhaustive handling of success and error cases.
public abstract record Result<T> : IEquatable<Result<T>>
Type Parameters
TThe type of the value returned on success.
- Inheritance
-
Result<T>
- Implements
-
IEquatable<Result<T>>
- Derived
- Inherited Members
Properties
ErrorInfo
Gets the error information from a failed result.
public Error ErrorInfo { get; }
Property Value
Exceptions
- InvalidOperationException
Thrown if this is a successful result.
ErrorOrDefault
Gets the error information if this is a failed result, otherwise null. Use this for safe access without exceptions. Prefer pattern matching for most cases.
public Error? ErrorOrDefault { get; }
Property Value
IsFailure
Gets a value indicating whether this result represents a failed outcome.
[MemberNotNullWhen(true, "ErrorOrDefault")]
public bool IsFailure { get; }
Property Value
IsSuccess
Gets a value indicating whether this result represents a successful outcome.
[MemberNotNullWhen(true, "ValueOrDefault")]
public bool IsSuccess { get; }
Property Value
Value
Gets the data value from a successful result.
public T Value { get; }
Property Value
- T
Exceptions
- InvalidOperationException
Thrown if this is a failure result.
ValueOrDefault
Gets the data value if this is a successful result, otherwise null. Use this for safe access without exceptions. Prefer pattern matching for most cases.
public T? ValueOrDefault { get; }
Property Value
- T
Methods
GetValueOrDefault(Func<T>)
Returns the success value if present, otherwise invokes the factory function to get a default value.
public T GetValueOrDefault(Func<T> defaultFactory)
Parameters
defaultFactoryFunc<T>A function that produces the default value.
Returns
- T
The success value or the result of the factory function.
Exceptions
- ArgumentNullException
Thrown when defaultFactory is null.
GetValueOrDefault(T)
Returns the success value if present, otherwise returns the specified default value.
public T GetValueOrDefault(T defaultValue)
Parameters
defaultValueTThe value to return if this is a failure.
Returns
- T
The success value or the default value.
Match<TResult>(Func<T, TResult>, Func<Error, TResult>)
Matches the result to one of two functions based on success or failure.
public TResult Match<TResult>(Func<T, TResult> onSuccess, Func<Error, TResult> onFailure)
Parameters
onSuccessFunc<T, TResult>Function to invoke if this is a success.
onFailureFunc<Error, TResult>Function to invoke if this is a failure.
Returns
- TResult
The result of the invoked function.
Type Parameters
TResultThe return type of the match functions.
Exceptions
- ArgumentNullException
Thrown when onSuccess or onFailure is null.
OnFailure(Action<Error>)
Executes an action if the result is a failure.
public Result<T> OnFailure(Action<Error> action)
Parameters
Returns
- Result<T>
This result for method chaining.
Exceptions
- ArgumentNullException
Thrown when action is null.
OnSuccess(Action<T>)
Executes an action if the result is successful.
public Result<T> OnSuccess(Action<T> action)
Parameters
actionAction<T>The action to execute with the success value.
Returns
- Result<T>
This result for method chaining.
Exceptions
- ArgumentNullException
Thrown when action is null.
SelectManyAsync<TResult>(Func<T, Task<Result<TResult>>>)
Asynchronously transforms the success value using a function that returns a new result.
public Task<Result<TResult>> SelectManyAsync<TResult>(Func<T, Task<Result<TResult>>> selector)
Parameters
selectorFunc<T, Task<Result<TResult>>>The async function to transform a successful value into a new result.
Returns
- Task<Result<TResult>>
A task containing the result of the selector function, or the original failure.
Type Parameters
TResultThe type of the value in the resulting Result.
Exceptions
- ArgumentNullException
Thrown when selector is null.
SelectMany<TResult>(Func<T, Result<TResult>>)
Transforms the success value using a function that returns a new result, enabling chaining of operations.
public Result<TResult> SelectMany<TResult>(Func<T, Result<TResult>> selector)
Parameters
Returns
- Result<TResult>
The result of the selector function, or the original failure.
Type Parameters
TResultThe type of the value in the resulting Result.
Exceptions
- ArgumentNullException
Thrown when selector is null.
Select<TResult>(Func<T, TResult>)
Transforms the success value using the provided function, or passes through the failure.
public Result<TResult> Select<TResult>(Func<T, TResult> selector)
Parameters
selectorFunc<T, TResult>The function to transform a successful value.
Returns
- Result<TResult>
A new result with the transformed value or the original failure.
Type Parameters
TResultThe type of the transformed value.
Exceptions
- ArgumentNullException
Thrown when selector is null.
TryGetError(out Error?)
Attempts to get the error information.
public bool TryGetError(out Error? error)
Parameters
errorErrorWhen this method returns, contains the error if the result is a failure; otherwise, null.
Returns
- bool
True if this result is a failure; otherwise, false.
TryGetValue(out T)
Attempts to get the success value.
public bool TryGetValue(out T value)
Parameters
valueTWhen this method returns, contains the success value if the result is successful; otherwise, the default value for type
T.
Returns
- bool
True if this result is successful; otherwise, false.