Search code examples
monadsreasoncomputation-expression

Does ReasonML have an equivalent of F#'s Computation Expressions?


In F# we have Computation Expressions, which can reduce boiler-plate and nesting when working in various computational contexts (async, optionals and so on).

  • Does ReasonML have an equivalent of this?
  • If so, what is the syntax?

Solution

  • By default, nope; no computation expressions. Reason is just another syntax still sharing all of the OCaml semantics, so a similar behavior is commonly achieved using modules, functors and composition.

    However, Reason and OCaml have ppx rewriters, that allow extending the syntax to achieve this sort of things. There is already an open issue about a special syntax for asynchronous and awaitable computations.

    Some useful ppx rewriters that are similar to computation expressions:

    • ppx_let: Monadic let-bindings
    • ppx_lwt: LWT programming, async/await
    • ppx_deriving: Haskell like type deriving, like for Show, Eqand Ord