Recursive types in nested modules

Hello,

I would need to create bindings for recursive types, but I want the types to be in a separate module:

module Foo = {
  module A = {
    type t = {b: B.t} // Gives error
  }

  module B = {
    type t = {a: A.t}
  }
}

Can I do this in ReScript? If not, how to work around this?

Modules can be recursive as well, but then you need to give them explicit module type definitions:

module Foo = {
  module rec A: {
    type t = {b: B.t}
  } = {
    type t = {b: B.t}
  }

  and B: {
    type t = {a: A.t}
  } = {
    type t = {a: A.t}
  }
}
3 Likes

Thanks, I gave up and went for @get instead:

module Foo = {
  type a
  type b

  module A = {
    @get
    external b: a => b = "b"
  }

  module B = {
    @get
    external a: b => b = "a"
  }
}

let a: Foo.a = %raw(`{}`)

let b = a->Foo.A.b
1 Like