Open! SomeModule vs open SomeModule


#1

What is the difference bewteen open! and open when opening a module?
Answer via @adagio in discord
Open will warn when currently visible names are shadowed/overridden by names in the open, while open! does not warn/tells compiler this is intended.

Any other thoughts?


#2

Yeah that’s it I think. Though try avoiding both!


Referencing [@bs.deriving abstract] type definitions in another Module
#3

Here is an example. You are suggesting to not use open so instead just call it directly where you need it then?

module Decode = {
    open! Json.Decode;
    let getTags = x =>
      Belt.List.map(
        Js.String.split(",", x) |> Belt.List.fromArray,
        Js.String.trim,
      );
    let convertRawToSnippet = value =>
      Js.Dict.{
        code: {
          re: "",
          js: "",
          test: "",
        },
        id: "base" |> get(value) |> Belt.Option.getExn(_),
        /* id: Belt.Option.getExn(get(value, "base")), */
        title: "title" |> get(value) |> Belt.Option.getExn(_),
        /* tags: getTags(get(value, "tags")), */
        tags: "tags" |> get(value) |> Belt.Option.getExn(_) |> getTags,
        description: "",
        notes: [],
      };
    let fileMap = json => json |> field("fileMap", dict(dict(string)));
  };

The last line would be let fileMap = json => json |> Json.Decode.field("fileMap", dict(dict(string))); with no open call.


#4

Local (non-top-level) opens are fine I guess.


#5

Would you prefer fully qualified names in all cases rather than local open, then?


#6

I’d prefer aliases:

module F = Foo.Bar.Baz;
let a = Qux.a;

Extending String module inside a utility module
#7

I am a bit slow today; where did Qux come from? Also, I am presuming you could then say:

let result = F.someFunction(3); /* as the equivalent of */
let result = Foo.Bar.Baz.someFunction(3);

#8

That’s correct - F is now an alias of Foo.Bar.Baz and can be used in its place.


#9

I believe Qux is meant to be another module available in scope, just like Foo. The example shows that you can create an alias of a whole module (F) or a single function (a).


#10

Nowhere. It’s another module. I was showing one module alias, and one binding alias