2

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?

csb
  • 674
  • 5
  • 13
  • From what I gather, `.{ ... }` is really a shorthand for `.({ ... })`, but I'll need someone who has more experience to confirm this. – csb Apr 17 '18 at 07:37

1 Answers1

2

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));
Javier Chávarri
  • 1,605
  • 11
  • 21