Repository F# setup
open Reified
open Reified.Refinements
open Reified.Result
open Reified.Schema.JsonExtracting values
At the edge of a pipeline you usually stop working in Result and hand a plain value to something that does not know
about it.
open Reified.Result
type SignupError = AgeMissing
ReifiedResult03-result-handling_40-extracting.md_page.SignupErrorAgeMissingA default
defaultValue returns the success value, or the fallback you supply.
Ok 36 |> Result.defaultValue 0 // 36
(Error AgeMissing: Result<int, SignupError>) |> Result.defaultValue 0 // 0
OkRepresents an OK or a Successful result. The code succeeded with a value of 'T.
(|>): 'T1 -> ('T1 -> 'U) -> 'UApply a function to a value, the value being on the left, the function on the right The argument. The function. The function result. let doubleIt x = x * 2 3 |> doubleIt // Evaluates to 6
Reified.Result.ResultModuleFail-fast helpers over the standard F# Result type.
defaultValue: 'value -> Result<'value,'error> -> 'valueReturns the success value or the supplied fallback value.
ErrorRepresents an Error or a Failure. The code failed with a value of 'TError representing what went wrong.
AgeMissingMicrosoft.FSharp.Core.FSharpResult`2Helper type for error handling without exceptions. Choices and Results
intAn abbreviation for the CLI type . Basic Types
03-result-handling_40-extracting.md_page.SignupErrorAn option
toOption and toValueOption drop the error entirely.
Ok 36 |> Result.toOption // Some 36
(Error AgeMissing: Result<int, SignupError>) |> Result.toOption // None
(Error AgeMissing: Result<int, SignupError>) |> Result.toValueOption // ValueNone
OkRepresents an OK or a Successful result. The code succeeded with a value of 'T.
(|>): 'T1 -> ('T1 -> 'U) -> 'UApply a function to a value, the value being on the left, the function on the right The argument. The function. The function result. let doubleIt x = x * 2 3 |> doubleIt // Evaluates to 6
Reified.Result.ResultModuleFail-fast helpers over the standard F# Result type.
toOption: Result<'value,'error> -> 'value optionDrops the error channel and returns Some for success.
ErrorRepresents an Error or a Failure. The code failed with a value of 'TError representing what went wrong.
AgeMissingMicrosoft.FSharp.Core.FSharpResult`2Helper type for error handling without exceptions. Choices and Results
intAn abbreviation for the CLI type . Basic Types
03-result-handling_40-extracting.md_page.SignupErrortoValueOption: Result<'value,'error> -> 'value voptionDrops the error channel and returns ValueSome for success.
These discard why the operation failed. That is the point when the caller genuinely does not care, but it is worth being deliberate: once the error is gone it cannot be reported, logged, or returned to a user.
Pattern matching
Nothing stops you matching the value directly, and for a final branch that handles both sides it is usually clearest:
match parseAge "abc" with
| Ok age -> printfn "age is %d" age
| Error failure -> printfn "rejected: %A" failureA Result from this package is the standard F# type, so every existing technique applies — matching, function
shorthand, active patterns.
Keeping the error
If you need the value and the reason it might be missing, do not extract at all. Keep the Result until the last
possible point, and let the caller decide. Extracting early is what forces the error back into a null, a sentinel,
or an exception.