Skip to content
Go to file

Deprecated, use the reasonml-community one


This is a ppx that looks for let%Anything, where Anything is a valid module identifier (e.g. captialized), and turns it into Anything.let_(value, pattern => continuation).

It's an exploration of this PR to the reason repo.

Here's an example of using it (source)

let%Opt node = get(store, id);
let%Opt parent = get(store, node.parent);
  [NodeChildren(, parent.children |. List.keep((!=)(, DeleteNode(,
  View({, active: nextId})
  [Event.Node(, Event.View(Node(nextId))]


Let's make a try_ function for the result type that does a bind over the error case:

module Res = {
  let try_ = (result, handler) => switch result {
    | Ok(v) => Ok(v)
    | Error(e) => handler(e)

This will come in handy for the case where we want to transform the error side of a result -- either turning it into a different error type, or turning it into a successful value. For example:

let v = Error("bad")
try%Res v {
  | "invalid username/password" => Error(UserError)
  | "no user - use default" => Ok(defaultUser)
  | message => Error(ServerError)

In the general case

If you have a data type that has a "failure" case (like promises, or the result type), the try%Anything syntax can be helpful. Here's the transform:

try%Blah someExpr {
  | Something(x) => y


Blah.try_(someExpr, result => switch result {
  | Something(x) => y


And here are the modules used (source)

module Opt = {
  let let_ = (a, b) => switch (a) {
    | None => None
    | Some(x) => b(x)

module Result = {
  let let_ = (value, fn) => switch value {
    | Ok(v) => fn(v)
    | Error(_) => value
  let try_ = (value, fn) => switch value {
    | Ok(_) => value
    | Error(err) => fn(err)


  1. Install the NPM package let-anything through your favorite npm package manager (npm itself or yarn for example)
  2. Add the following path to the ppx-flags defined in your bsconfig.json file (see let-anything/lib/bs/native/let_anything.native


Deprecated, use the reasonml-community one



No releases published


No packages published
You can’t perform that action at this time.