Repository F# setup
open Reified
open Reified.Refinements
open Reified.ResultResult handling
Maturity: Incubating
Reified.Result works with the standard F# Result<'value, 'error>. Other F# code can pattern match its Ok and Error values.
Its module and DSL convert values to a Result, chain steps, replace errors, extract values, and provide two
computation expressions for fallible steps.
dotnet add package Reified.Result
A first example
open System
open Reified.Result
open Reified.ResultDSL
type SignupError =
| NameMissing
| AgeNotANumber of string
| AgeOutOfRange of int
let parseName (raw: string) =
raw
|> Result.failIf String.IsNullOrWhiteSpace
|> Result.orError NameMissing
let parseAge (raw: string) =
Int32.TryParse raw
|> Result.fromTry
|> Result.orError (AgeNotANumber raw)
|> Result.bind (fun age ->
if age >= 0 && age < 130 then Ok age else Error (AgeOutOfRange age))
result {
let! name = parseName "Ada"
let! age = parseAge "36"
return {| Name = name; Age = age |}
}
SystemReifiedResultReified.ResultDSLThe concise result vocabulary: the result { } computation expression, its accumulating result.list { } / result.array { } variants, and the lightweight admission functions (okIf, failIf, require, orError, mapError). Optional and opt-in, in the same shape as Reified.DataDSL, Reified.ConstraintDSL, and Reified.SchemaDSL: open Reified.Result for Result, then open Reified.ResultDSL for this vocabulary. Deliberately small: generic combinators such as map, bind, orElse, tap, and the traversal helpers stay qualified as Result.map, Result.bind, and so on.
FsLiveDocsGeneratedPage19_F2D257DD2F93.SignupErrorNameMissingAgeNotANumberstringAn abbreviation for the CLI type . Basic Types
AgeOutOfRangeintAn abbreviation for the CLI type . Basic Types
parseName: string -> Result<string,SignupError>raw: string(|>): '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.
failIf: ('input -> bool) -> 'input -> Result<'input,unit>Keeps the input value when the predicate does not hold, or returns the supplied error. The inverse of okIf: fails when the predicate is true, succeeds otherwise.
System.StringRepresents text as a sequence of UTF-16 code units.
IsNullOrWhiteSpace: string -> boolIndicates whether a specified string is , empty, or consists only of white-space characters. The string to test. if the parameter is or , or if consists exclusively of white-space characters.
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.
parseAge: string -> Result<int,SignupError>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, .
fromTry: bool * 'value -> Result<'value,unit>Converts a .NET Try* tuple into a unit-error result.
bind: ('a -> Result<'b,'c>) -> Result<'a,'c> -> Result<'b,'c>Binds a result to the next fail-fast operation.
age: int(>=): 'T -> 'T -> boolStructural greater-than-or-equal The first parameter. The second parameter. The result of the comparison. 5 >= 1 // Evaluates to true 5 >= 5 // Evaluates to true [1; 5] >= [1; 6] // Evaluates to false
(&&): bool -> bool -> boolBinary 'and'. When used as a binary operator the right hand value is evaluated only on demand The first value. The second value. The result of the operation.
(<): 'T -> 'T -> boolStructural less-than comparison The first parameter. The second parameter. The result of the comparison. 1 < 5 // Evaluates to true 5 < 5 // Evaluates to false (1, "a") < (1, "z") // Evaluates to true
OkRepresents an OK or a Successful result. The code succeeded with a value of 'T.
ErrorRepresents an Error or a Failure. The code failed with a value of 'TError representing what went wrong.
result: ResultBuilderThe fail-fast result { } computation expression.
name: stringName: stringAge: intOk { Name = "Ada"; Age = 36 }
Pages
- Creating a Result - turn options,
TryParsetuples, booleans, and predicates into aResultwith your own error type. - Transforming values -
mapandbind, and how a chain of fallible steps compose. - Handling errors - change the error type, replace one, and recover.
- Extracting values - get back to a plain value, an option, or a default.
- Working with collections, apply a fallible operation across a sequence with
traverseandsequence, or collect every failure withtraverseAllandsequenceAll. - Observing a Result - log or measure mid-pipeline with
tapandtapError. - The result computation expression - write dependent steps as straight-line code with
result { }. - Collecting every error - report all independent failures at once with
result.list { }andand!. - Comparison with FsToolkit.ErrorHandling - what each library is for, and how they interoperate.
- API reference - every function, generated from the source.
Related
Reified.Result composes failures. Constraints test typed values,
Parsing decodes serialized primitives, and Refined constructs values
whose types record a successful check. All three return the
standard F# Result, so these helpers work on their output, but none requires this package, and this package does
not require them.
Accumulation here is flat: result.list { } collects a list of your error values with no field identity.
When a whole form, request, or document must become a model with path-aware accumulated diagnostics, that is
Reified.Schema.