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.
('okInput -> Validation<'okOutput, 'error>) -> Validation<'okInput, 'error> -> Validation<'okOutput, 'error>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"let result =
Validation.ok "42"
|> Validation.bind tryParseInt
// Ok 42let result =
Validation.ok "not a number"
|> Validation.bind tryParseInt
// Error ["'not a number' is not a valid integer"]let result =
Validation.error "previous error"
|> Validation.bind tryParseInt
// Error ["previous error"]