2

I'm working on a bucklescript binding to leafletjs based on this project .

With leaflet a Map has a function to add layer and a Layer has a function to add itself to a map.

This is what I would like to achieve with ReasonML :

module Map = {
    type t;
    [@bs.send] external addLayer : (t, Layer.t) => t = "addLayer";
};

module Layer = {
    type t;
    [@bs.send] external addTo : Map.t => unit = "addTo";
};

Unfortunately I get an unbound module Layer error.

How do I make the compiler aware of the type described after ?

glennsl
  • 28,186
  • 12
  • 57
  • 75
Yvain
  • 882
  • 1
  • 10
  • 27

1 Answers1

7

Option 1: Define the types in a common module and alias them:

type map;
type layer;

module Map = {
    type t = map;
    [@bs.send] external addLayer : (t, layer) => t = "addLayer";
};

module Layer = {
    type t = layer;
    [@bs.send] external addTo : map => unit = "addTo";
};

Option 2: Make the modules mutually recursive:

module rec Map : {
    type t;
    [@bs.send] external addLayer : (t, Layer.t) => t = "addLayer";
} = {
    type t;
    [@bs.send] external addLayer : (t, Layer.t) => t = "addLayer";
}

and  Layer : {
    type t;
    [@bs.send] external addTo : Map.t => unit = "addTo";
} = {
    type t;
    [@bs.send] external addTo : Map.t => unit = "addTo";
};
glennsl
  • 28,186
  • 12
  • 57
  • 75
  • 2
    @Yvain - cool shortcut–if the modules are recursive and just define types and externals you can just make them refer to themselves: `module rec Map: {...} = Map and Layer: {...} = Layer;` – Yawar Dec 13 '19 at 14:49