Result¶
Result and Result<T> are unions that hold the outcome of an operation: a value (or Ok) when it worked, an Exception when it failed. The operators in Fluent.Unions chain results without try/catch and without null checks.
In this section¶
- Core concepts: the two states, creating results, members,
switch, deconstruction andCheck(). - Operations:
Select,Chain,Zip,Ensure,CombineAll,ChainAppend,Do,Tap,Out,Finally,GetValueOrDefault,Unwrap,Match. - Error handling:
Catch,OnErrorDo,Try,Retry, and what still throws. - Collections:
Combine,CombineAll,Partition,ValuesandErrorsfor batches. - Patterns: railway pipelines, query syntax, early returns, service layers, and when not to use results.
Quick example¶
using Fluent.Unions;
Result<string> label = GetCustomer(id)
.Chain(GetCart)
.Chain(PlaceOrder)
.Select(order => $"Order {order.Id}")
.Catch(e => "Checkout failed");
When to use¶
- A method can fail and the caller should look at the outcome.
- You want to combine several fallible steps into one expression.
- You want the compiler to check that every state is handled.
Use Option<T> instead when a missing value is normal and carries no error. See Option.
Rules¶
- A result has two states: ok and error. A
defaultresult counts as an error. See Core concepts. - Operators on results never throw because of an exception inside your lambda. The exception becomes the error. Cancellation (
OperationCanceledException) is the exception: it propagates as usual.Tap,TapErrorandTapAwaitnever capture, andCheck()throws the error on purpose. See Error handling. - A
nulllambda argument throwsArgumentNullExceptionimmediately. - A
nullvalue, whether returned from a selector or converted implicitly, gives an error. UseOption<T>for missing values. Matchon a result takes two callbacks:onSuccessandonError.- Ignoring a returned result gives analyzer warning FLU0001.
Back to index: Docs index ยท Next: Core concepts