Search code examples
moduledependenciesocamlreasonbucklescript

ReasonML cycle dependency


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 ?


Solution

  • 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";
    };