DEV Community

loading...

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

Alain
・1 min read

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

Discussion (0)