Extending String module inside a utility module


#1

Inside a file called Util.re, I did this:

module String = {
  include String;

  /* Convert Unicode code point to corresponding string */
  [@bs.val] external fromCodePoint: int => string = "String.fromCodePoint";
  
  /* some more stuff */
};

In other source files, if I first do open Util; I can use my new function as if it were part of the String module, as well as access all the other functions that are under String. Is this good practice? Or should I my String module be renamed to something else, e.g. String_?


#2

I’ve heard it’s best to avoid using open.
Some people use an alias pattern instead:

module S = Util.String;
S.fromCodePoint(1);

Personally I just explicitly define the path most of the time.

Util.String.fromCodePoint(1);

Looking through this thread may give you a more authoritative answer than mine.


#3

Thanks for referring that thread! After reading it, I changed my Util.re to this:

module String_ = {
  include String;

  /* Convert Unicode code point to corresponding string */
  [@bs.val] external fromCodePoint: int => string = "String.fromCodePoint";

  /* other stuff */
};

And in the one module where I use Util.String_ functions extensively, I put this at the top:

module String = Util.String_;

It’s possibly still questionable, but I think this is much more explicit than the previous version.


#4

No, keep String, not String_. The two modules are not clashing. And it works better if someone decides to open Util. That’s how you are supposed to extend modules.