Skip to content

Commit 3d3d239

Browse files
authored
Add module destructuring docs to module page (#440)
* Update module.mdx * Update example
1 parent ccb3742 commit 3d3d239

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

pages/docs/manual/latest/module.mdx

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,42 @@ let arr = map([1,2,3], (a) => { a + 1})
157157

158158
**Note:** Same as with `open`, don't overuse `open!` statements if not necessary. Use (sub)modules to prevent shadowing issues.
159159

160+
### Destructuring modules
161+
162+
**Since 9.0.2**
163+
164+
As an alternative to `open`ing a module, you can also destructure a module's functions and values into separate let bindings (similarly on how we'd destructure an object in JavaScript).
165+
166+
<CodeTab labels={["ReScript", "JS Output"]}>
167+
168+
```res
169+
module User = {
170+
let user1 = "Anna"
171+
let user2 = "Franz"
172+
}
173+
174+
// Destructure by name
175+
let {user1, user2} = module(User)
176+
177+
// Destructure with different alias
178+
let {user1: anna, user2: franz} = module(User)
179+
```
180+
181+
```js
182+
var user1 = "Anna";
183+
184+
var user2 = "Franz";
185+
186+
var User = {
187+
user1: user1,
188+
user2: user2
189+
};
190+
```
191+
192+
</CodeTab>
193+
194+
**Note:** You can't extract types with module destructuring — use a type alias instead (`type user = User.myUserType`).
195+
160196
### Extending modules
161197

162198
Using `include` in a module statically "spreads" a module's content into a new one, thus often fulfill the role of "inheritance" or "mixin".

0 commit comments

Comments
 (0)