Source

spotlib / lib / option.ml

The default branch has multiple heads

Full commit
type 'a t = 'a option

include Monad.Make(struct
  type 'a t = 'a option

  let return v = Some v

  let bind t f = match t with
    | Some v -> f v
    | None -> None
end)

let default v df = match v with
  | None -> df ()
  | Some v -> v

let catch f =
  let module Error = struct exception Error end in
  let fail () = raise Error.Error in
  try Some (f ~fail) with Error.Error -> None

let catch_exn f = catch (fun ~fail -> try f () with _ -> fail ())

let from_Some = function
  | Some v -> v
  | _ -> invalid_arg "Optino.from_Some"