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
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:
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)
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.