Repository F# setup
open Reified
open Reified.Refinements
open Reified.ResultExtracting values
At the edge of a pipeline, extract a plain value when the caller does not handle Result.
open Reified.Result
type SignupError = AgeMissing
ReifiedResultFsLiveDocsGeneratedPage14_9A5DDEA924C8.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
FsLiveDocsGeneratedPage14_9A5DDEA924C8.SignupErrorRequires & types
Requires: open Reified.Result (packages: Reified.Result)An 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
FsLiveDocsGeneratedPage14_9A5DDEA924C8.SignupErrortoValueOption: Result<'value,'error> -> 'value voptionDrops the error channel and returns ValueSome for success.
Requires & types
Requires: open Reified.Result (packages: Reified.Result)These discard why the operation failed. Use them when the caller does not need the reason: once the error is gone, it cannot be reported, logged, or returned to a user.
Pattern matching
Pattern match when the final branch handles both sides:
open System
let parseAge (raw: string) =
Int32.TryParse raw
|> Result.fromTry
|> Result.orError AgeMissing
match parseAge "abc" with
| Ok age -> printfn "age is %d" age
| Error failure -> printfn "rejected: %A" failure
SystemparseAge: string -> Result<int,SignupError>raw: stringstringAn abbreviation for the CLI type . Basic Types
System.Int32Represents a 32-bit signed integer.
TryParse: string * byref<int> -> boolConverts the string representation of a number to its 32-bit signed integer equivalent. A return value indicates whether the conversion succeeded. A string containing a number to convert. When this method returns, contains the 32-bit signed integer value equivalent of the number contained in , if the conversion succeeded, or zero if the conversion failed. The conversion fails if the parameter is or , is not of the correct format, or represents a number less than Int32.MinValue or greater than Int32.MaxValue. This parameter is passed uninitialized; any value originally supplied in will be overwritten. if was converted successfully; otherwise, .
(|>): '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.
fromTry: bool * 'value -> Result<'value,unit>Converts a .NET Try* tuple into a unit-error result.
orError: 'error -> Result<'value,'discardedError> -> Result<'value,'error>Replaces whatever error a result carries with the supplied typed error. Ok passes through unchanged. The natural follow-up to okIf/failIf, which fail with unit precisely so the reason is chosen here: value |> Result.okIf isValid |> Result.orError MyError. Use Result.mapError instead when the existing error carries something worth keeping, as a Violation does.
AgeMissingOkRepresents an OK or a Successful result. The code succeeded with a value of 'T.
age: intprintfn: Printf.TextWriterFormat<'T> -> 'TPrint to stdout using the given format, and add a newline. The formatter. The formatted result. See Printf.printfn (link: ) for examples.
ErrorRepresents an Error or a Failure. The code failed with a value of 'TError representing what went wrong.
failure: SignupErrorRequires & types
Requires: open Reified.Result; open System (packages: Reified.Result)val parseAge: string -> Result<int,SignupError>A 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.