What is the difference between .() and .{} in Reason?

I'm trying to figure out why the example for using Js.Promise uses

Js.Promise.(
    ...
)

Whereas the example for Json.Decode uses

Json.Decode.{
    ...
}

From what I understand, .() opens up Js.Promise so that I can just call any function within Js.Promise without having to prefix Js.Promise as the module.

But what does .{} do?

1 answer

  • answered 2018-04-17 05:39 Javier Chávarri

    Both put some module declarations in scope, but .{} creates a record while .() wraps an expression.

    For records:

    let point = json =>
      Json.Decode.{
        x: json |> field("x", float),
        y: json |> field("y", float)
      };
    

    is equivalent to:

    let point = json =>
      {
        x: json |> Json.Decode.field("x", Json.Decode.float),
        y: json |> Json.Decode.field("y", Json.Decode.float)
      };
    

    Similarly, for expressions:

    let _ =
      Js.Promise.(then_(value => resolve(Js.log(value)), okPromise));
    

    is equivalent to:

    let _ =
      Js.Promise.then_(value => Js.Promise.resolve(Js.log(value)), okPromise));