Skip to content

jaredly/let-anything

Repository files navigation

Deprecated, use the reasonml-community one

let%Anything

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);
Some((
  [NodeChildren(parent.id, parent.children |. List.keep((!=)(node.id))), DeleteNode(node.id),
  View({...store.view, active: nextId})
  ],
  [Event.Node(parent.id), Event.View(Node(nextId))]
))

try%Anything

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
}

becomes

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

Addendum

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)
  };
}

Usage

  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 bucklescript.github.io/bucklescript/docson/#build-schema.json): let-anything/lib/bs/native/let_anything.native

About

Deprecated, use the reasonml-community one

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published