package jsonxt

  1. Overview
  2. Docs

Source file error_or.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
module type IO = Io.IO

module type Intf = sig
  module IO : IO

  val return : 'a -> ('a, 'b) result IO.t
  val fail : 'b -> ('a, 'b) result IO.t
  val (>>=?)
    :  ('a, 'b) result IO.t
    -> ('a -> ('c, 'b) result IO.t)
    -> ('c, 'b) result IO.t
end

module Make (IO : IO) : Intf with module IO := IO = struct
  let return v = IO.return (Ok v)
  let fail err = IO.return (Error err)

  let (>>=?) a f =
    let open IO in
    a >>= fun a ->
    match a with
    | Ok a -> f a
    | Error err -> IO.return (Error err)
end