DEV Community

Alain
Alain

Posted on

Using `let.opt` in Rescript with latest Reason/OCaml

This is your Reason (let.opt) definition.

let (let.opt) = (x, f) =>
  switch (x) {
  | None => None
  | Some(x) => f(x)
  };
let (and.opt) = (a, b) =>
  switch (a, b) {
  | (Some(a), Some(b)) => Some((a, b))
  | _ => None
  };
let z = (a,b)=> {
  let.opt a = a
  and.opt b = b;
  Some(a + b);
};
Enter fullscreen mode Exit fullscreen mode

This is your Rescript file on ocaml@4.12.0:

//Demo.res
let a = Some(10);
let b = Some(3);

Js.log2("using ocaml 4.12 compiled reasonml let.opt in Rescript", Opts.z(a,b));
Enter fullscreen mode Exit fullscreen mode

Here is a gist.

Here is a project you can run:

GitHub logo idkjs / letop-rescript

using (let.opt) style in rescript

Resources: https://github.com/ocaml/ocaml/pull/1947
Melange:https://github.com/melange-re/melange
Rescript:https://rescript-lang.org/
Discord:https://discord.gg/KAJBzYaf

Top comments (1)

Collapse
 
danielo515 profile image
Daniel Rodríguez Rivero

Do you mean explaining how is this applied ? And expects a and b, and returns a tuple, which I don’t see used in z nor in the first letop. Can you expand further ?