Skip to main content

Result<T> API Reference

Complete reference for Result<T> and the static Result class.

Result<T> struct

public readonly struct Result<T>

Properties

PropertyTypeDescription
IsSuccessbooltrue if the operation succeeded
IsFailurebooltrue if the operation failed
ValueTThe success value. Throws SpurException if failed.
ErrorErrorThe error. Throws SpurException if succeeded.

Methods

MethodReturnsDescription
GetValueOrDefault(defaultValue?)T?Value if success, otherwise defaultValue (or default(T))
GetErrorOrDefault(defaultError?)ErrorError if failure, otherwise defaultError (or default)
Match(onSuccess, onFailure)TResultBranch on success/failure, return a value
MatchAsync(onSuccess, onFailure)Task<TResult>Async branch
Switch(onSuccess, onFailure)voidBranch on success/failure, perform actions
SwitchAsync(onSuccess, onFailure)TaskAsync action branch
Unwrap()TValue if success, throws SpurException if failed
UnwrapOr(fallback)TValue if success, otherwise fallback
UnwrapOrElse(fallback)TValue if success, otherwise compute fallback from error

Implicit conversions

// Value → Result<T> (success)
Result<int> result = 42;

// Error → Result<T> (failure)
Result<int> result = Error.NotFound("Not found");

Static Result class

Success factories

MethodReturnsDescription
Success<T>(value)Result<T>Create a success with a value
Success()Result<Unit>Create a success with no value

Failure factories

MethodReturnsDescription
Failure<T>(error)Result<T>Create a failure from an Error
Failure<T>(code, message, httpStatus?)Result<T>Create a failure from components

Pipeline entry points

MethodReturnsDescription
Start<T>(value)Result<T>Begin a pipeline with a value
StartAsync<T>(factory)Task<Result<T>>Begin a pipeline with an async factory

Try wrappers

MethodReturnsDescription
Try<T>(func)Result<T>Execute, catch exceptions as Error.Unexpected
TryAsync<T>(func)Task<Result<T>>Async version

Combine

MethodReturnsDescription
Combine<T>(results[])Result<IReadOnlyList<T>>Fail-fast: returns first failure
CombineAll<T>(results[])Result<IReadOnlyList<T>>Collect all failures into one error
Combine<T1,T2>(r1, r2)Result<(T1,T2)>Combine two different types
Combine<T1,T2,T3>(r1, r2, r3)Result<(T1,T2,T3)>Combine three different types

Pipeline operators

All pipeline operators are extension methods in the Spur.Pipeline namespace.

OperatorPurposeInputOutput
ThenChain fallible opResult<TIn>Result<TOut>
ThenAsyncAsync chainTask<Result<TIn>>Task<Result<TOut>>
MapTransform valueResult<TIn>Result<TOut>
MapAsyncAsync transformTask<Result<TIn>>Task<Result<TOut>>
ValidateAssert conditionResult<T>Result<T>
ValidateAsyncAsync assertTask<Result<T>>Task<Result<T>>
TapSide effect on successResult<T>Result<T>
TapErrorSide effect on failureResult<T>Result<T>
TapBothSide effect on eitherResult<T>Result<T>
TapAsyncAsync side effectTask<Result<T>>Task<Result<T>>
RecoverHandle errorResult<T>Result<T>
RecoverIfHandle specific categoryResult<T>Result<T>
RecoverIfCodeHandle specific codeResult<T>Result<T>
RecoverAsyncAsync recoveryTask<Result<T>>Task<Result<T>>
MatchTerminal branchResult<T>TResult
MatchAsyncAsync terminalTask<Result<T>>Task<TResult>

SpurException

public sealed class SpurException : Exception
{
public Error Error { get; }
}

Thrown when accessing .Value on a failed Result or .Error on a successful Result.

See also