Computation Expression

Namespace: FsToolkit.ErrorHandling

Examples

Example 1

let add x y z = x + y + z

let addResult : Result<int option, string> = resultOption {
  let! x = Ok (Some 30)
  let! y = Ok (Some 10)
  let! z = Ok (Some 2)
  return add x y z
}
// Ok (Some 42)

Example 2

let add x y z = x + y + z

let addResult : Result<int option, string> = resultOption {
  let! x = Ok (Some 30)
  let! y = Error "Oops 1"
  let! z = Error "Oops 2"
  return add x y z
}
// Error "Oops 1"

Example 3

The ResultOption.map2 example can be written using the resultOption computation expression as below

Last updated