[syntax] Add let binding syntax to IOption

Summary:
This adds `let*/+`, `and*/+` operators for Option. See [the manual](https://caml.inria.fr/pub/docs/manual-ocaml/manual046.html) for more information.

Example usage:
```
let foo =
  let open IOption.Let_syntax in
  let* a = get_optional () in
  let* b = get_another_optional () in
  return (a + b)
```

Reviewed By: jvillard

Differential Revision: D19880033

fbshipit-source-id: c7998b0c6
master
Artem Pianykh 5 years ago committed by Facebook Github Bot
parent 44f41d2929
commit b41064149b

@ -14,3 +14,15 @@ let value_default_f ~f = function None -> f () | Some v -> v
let if_none_evalopt ~f x = match x with None -> f () | Some _ -> x let if_none_evalopt ~f x = match x with None -> f () | Some _ -> x
let if_none_eval = value_default_f let if_none_eval = value_default_f
module Let_syntax = struct
include Option.Let_syntax
let ( let+ ) x f = Option.map ~f x
let ( and+ ) x y = Option.both x y
let ( let* ) x f = Option.bind ~f x
let ( and* ) x y = Option.both x y
end

@ -21,3 +21,20 @@ val if_none_eval : f:(unit -> 'a) -> 'a option -> 'a
(** [if_none_eval ~f x] evaluates to [y] if [x=Some y] else to [f ()]. Useful for terminating chains (** [if_none_eval ~f x] evaluates to [y] if [x=Some y] else to [f ()]. Useful for terminating chains
built with [if_none_evalopt]. This is exactly the same as [value_default_f] but with a better built with [if_none_evalopt]. This is exactly the same as [value_default_f] but with a better
name. *) name. *)
include sig
[@@@warning "-32-60"]
(** Provides signatures for OCaml 4.08 binding operators *)
module Let_syntax : sig
include module type of Option.Let_syntax
val ( let+ ) : 'a option -> ('a -> 'b) -> 'b option
val ( and+ ) : 'a option -> 'b option -> ('a * 'b) option
val ( let* ) : 'a option -> ('a -> 'b option) -> 'b option
val ( and* ) : 'a option -> 'b option -> ('a * 'b) option
end
end

Loading…
Cancel
Save