Skip to content

Latest commit

 

History

History
53 lines (37 loc) · 1.18 KB

File metadata and controls

53 lines (37 loc) · 1.18 KB

Validation.bind

Namespace: FsToolkit.ErrorHandling

bind applies a function to the value inside a Validation if it is Ok, returning the resulting Validation. Unlike apply and map2/map3, bind uses monadic (short-circuit) semantics — if the input is an Error, the binder function is not called and the error is returned immediately.

Function Signature

('okInput -> Validation<'okOutput, 'error>) -> Validation<'okInput, 'error> -> Validation<'okOutput, 'error>

Examples

Take the following function for example:

// string -> Validation<int, string>
let tryParseInt (s: string) =
    match System.Int32.TryParse(s) with
    | true, i -> Validation.ok i
    | false, _ -> Validation.error $"'%s{s}' is not a valid integer"

Example 1

let result =
    Validation.ok "42"
    |> Validation.bind tryParseInt

// Ok 42

Example 2

let result =
    Validation.ok "not a number"
    |> Validation.bind tryParseInt

// Error ["'not a number' is not a valid integer"]

Example 3

let result =
    Validation.error "previous error"
    |> Validation.bind tryParseInt

// Error ["previous error"]