Skip to content

Commit ae5d3a8

Browse files
authored
Merge pull request #316 from kevanstannard/syntax-lookup-include
Syntax lookup: Include
2 parents b8c596d + a7a9755 commit ae5d3a8

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed

misc_docs/syntax/language_include.mdx

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
---
2+
id: "include"
3+
keywords: ["include", "module"]
4+
name: "include"
5+
summary: "This is the `include` keyword."
6+
category: "languageconstructs"
7+
---
8+
9+
The `include` keyword statically "spreads" all public values, types, modules, etc. of a given module into the current module's scope. This is sometimes useful to create supersets / mixins of different modules.
10+
11+
> Note that `include` may make your code more complex and harder to understand when abused. Think twice before using it.
12+
13+
### Include module example
14+
15+
<CodeTab labels={["ReScript", "JS Output"]}>
16+
17+
```res
18+
module Message = {
19+
let greeting = "Hello"
20+
}
21+
22+
module Greeter = {
23+
include Message
24+
let greet = name => greeting ++ " " ++ name
25+
}
26+
```
27+
28+
```js
29+
var greeting = "Hello";
30+
31+
var Message = {
32+
greeting: greeting,
33+
};
34+
35+
function greet(name) {
36+
return "Hello " + name;
37+
}
38+
39+
var Greeter = {
40+
greeting: greeting,
41+
greet: greet,
42+
};
43+
```
44+
45+
</CodeTab>
46+
47+
Similarly, module signatures can also be extended by other module signatures.
48+
49+
### Include signature example
50+
51+
<CodeTab labels={["ReScript", "JS Output"]}>
52+
53+
```res
54+
module type Message = {
55+
let greeting: string
56+
}
57+
58+
module type Greeter = {
59+
include Message
60+
let greet: string => string
61+
}
62+
```
63+
64+
```js
65+
// No output
66+
```
67+
68+
</CodeTab>
69+
70+
Lastly, you can also extract module types from existing modules using `module type of`.
71+
72+
### Include "module type of" example
73+
74+
<CodeTab labels={["ReScript", "JS Output"]}>
75+
76+
```res
77+
module Message = {
78+
let greeting = "Hello"
79+
}
80+
81+
module type Greeter = {
82+
// Includes the type definitions of Message
83+
include module type of Message
84+
let greet: string => string
85+
}
86+
```
87+
88+
```js
89+
var Message = {
90+
greeting: "Hello"
91+
};
92+
```
93+
94+
</CodeTab>
95+
96+
97+
### References
98+
99+
* [Extending modules](/docs/manual/latest/module#extending-modules)
100+
* [Extending module signatures](/docs/manual/latest/module#extending-module-signatures)

0 commit comments

Comments
 (0)