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);
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))]
))
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)
}
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
})
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)
};
}
- Install the NPM package
let-anything
through your favoritenpm
package manager (npm itself or yarn for example) - Add the following path to the
ppx-flags
defined in yourbsconfig.json
file (see bucklescript.github.io/bucklescript/docson/#build-schema.json):let-anything/lib/bs/native/let_anything.native