Tip to import several elements from the same module in one line


#1

I share a tip with you that allows us to import several elements (function, value etc.) from a single module at once.

Let be the following module:

module ModuleA = {
  let valueA = "A";
  let functionA = a => a;

  let valueB = "B";
  let functionB = b => b;

  let valueC = "C";
  let functionC = c => c;
};

If I want to import *A and *B but not *C, I do:

let (valueA, functionA, valueB, functionB) = ModuleA.(valueA, functionA, valueB, functionB);

Wich is equivalent to:

let valueA = ModuleA.valueA;
let functionA = ModuleA.functionA;
let valueB = ModuleA.valueB;
let functionB = ModuleA.functionB;

#2

It’s a pity you can’t just destructure a module like a record, e.g.:

let { valueA, functionA, valueB, functionB } = ModuleA;

Why is that, I wonder.


#3

The reason syntax doesn’t allow it.
It would be interesting to submit the idea to the reason core.


#4

@maarekj we won’t be supporting the record syntax just yet, since it’s also a bit confusing to newcomers (records vs modules). But that trick you used is actually a very intentional one from us: https://github.com/facebook/reason/pull/623


#5

@chenglou As a newcomer from JS/TS with very little ML knowledge I find the module as record concept more familiar :slight_smile: (cp. import { foo, bar } from 'src/utils/Module')

But records vs tuples is not what really bothers me personally. Elm, for instance, does use tuple syntax, but I find its imports more concise: import Html as H exposing (div, span) etc, not (div, span) = (div, span).