Syntax for first-class modules

I know how to use first-class modules with the ReasonML syntax, and I know how to “pack” a module in ReScript. But I’m not sure what the syntax is for “unpacking” a module in ReScript. Can somebody post an example of how to do this?

As it’s not in the docs yet, I found Alex’s article pretty helpful

// Packing
let human = module(Teenager)

// Unpacking
let module(Teenager) = human
2 Likes

Thanks @fham, that certainly helps. However, I can’t seem to get this to work in the top-level module scope.

Here’s an example:

https://rescript-lang.org/try?code=PTAEAUBsFMEMGdqgHYHsAu0BcAoEoBaIvMAFQAskpYBPAcwCdUBXZAE1AEt5R51PIkULFAB3VAwDWXZKAAOTRtHjwSoAErQAygGMGnOelBhUkDpoSpZ8GsnSwAHr3Isz82A0SgAVsz5qAM05kJAAKOlRQdEj0SlAAIi1odH5kOnh49xChAIko1Do6SGC6Xlt7BwBKADoSNQAxaGgchib8+UhaYSZWDlh2UB1UAFs5ASR4Eeg1TV19Q0HUNmgAQhwcYaXmGCiaOSQAQVAAXlAAbxxQXf2ok5l0S9AYIwA3WEhmbCicAF91zbY20OWFAR1OFyu6D2SCMp2CDyuz1Abw+X1hoAALAAmX7rJFyWA6STQNhg0AAoGhA4gg6VPHJclbGChACqyAJRJJtLuHOJpPWQA

I really need the equivalent of this (in ReasonML syntax):

module UnpackedA: A = (val packedA)

Note that this is different from the following code (also in ReasonML syntax):

let (module UnpackedA: A) = packedA

Nevermind. I figured it out by writing it in ReasonML syntax and converting it to ReScript using the conversion tool.

There is a new keyword called unpack, which serves the same purpose as val in the ReasonML version. It looks like this:

module type A = {
  type t = int
  let value: t
}

module A: A = {
  type t = int
  let value: t = 42
}

let packedA = module(A: A)

/*
  equivalent to:
  `module UnpackedA: A = (val packedA)`
*/
module UnpackedA: A = unpack(packedA)
4 Likes

Huh? The keyword was there from the start? I could swear that is new.

Anyways thanks for figuring out. Could be a good basis for the corresponding docs issue.

Technically speaking, it’s an “unreserved keyword” (so it doesn’t collide with binding names etc). It was never mentioned because there are no FCM docs yet, and we also never promoted that feature.

2 Likes