File tree Expand file tree Collapse file tree 1 file changed +100
-0
lines changed Expand file tree Collapse file tree 1 file changed +100
-0
lines changed Original file line number Diff line number Diff line change
1
+ ---
2
+ id : " include"
3
+ keywords : ["include", "module"]
4
+ name : " include"
5
+ summary : " This is the `include` keyword."
6
+ category : " languageconstructs"
7
+ ---
8
+
9
+ Using ` include ` in a module statically "spreads" another module's content into the module, thus often fulfills the role of "inheritance" or "mixin".
10
+
11
+ ### Include module example
12
+
13
+ <CodeTab labels = { [" ReScript" , " JS Output" ]} >
14
+
15
+ ``` res
16
+ module Message = {
17
+ let greeting = "Hello"
18
+ }
19
+
20
+ module Greeter = {
21
+ include Message
22
+ let greet = name => greeting ++ " " ++ name
23
+ }
24
+ ```
25
+
26
+ ``` js
27
+ var greeting = " Hello" ;
28
+
29
+ var Message = {
30
+ greeting: greeting,
31
+ };
32
+
33
+ function greet (name ) {
34
+ return " Hello " + name;
35
+ }
36
+
37
+ var Greeter = {
38
+ greeting: greeting,
39
+ greet: greet,
40
+ };
41
+ ```
42
+
43
+ </CodeTab >
44
+
45
+ Similarly, module signatures can also be extended by other module signatures.
46
+
47
+ ### Include signature example
48
+
49
+ <CodeTab labels = { [" ReScript" , " JS Output" ]} >
50
+
51
+ ``` res
52
+ module type Message = {
53
+ let greeting: string
54
+ }
55
+
56
+ module type Greeter = {
57
+ include Message
58
+ let greet: string => string
59
+ }
60
+ ```
61
+
62
+ ``` js
63
+ // No output
64
+ ```
65
+
66
+ </CodeTab >
67
+
68
+ Lastly, you can also extract module types from existing modules using ` module type of ` .
69
+
70
+ ### Include "module type of" example
71
+
72
+ <CodeTab labels = { [" ReScript" , " JS Output" ]} >
73
+
74
+ ``` res
75
+ module Message = {
76
+ let greeting = "Hello"
77
+ }
78
+
79
+ module type Greeter = {
80
+ // Includes the type definitions of Message
81
+ include module type of Message
82
+ let greet: string => string
83
+ }
84
+ ```
85
+
86
+ ``` js
87
+ var Message = {
88
+ greeting: " Hello"
89
+ };
90
+ ```
91
+
92
+ </CodeTab >
93
+
94
+ > Note that use of ` include ` is heavily discouraged and should only be used as a last resort.
95
+
96
+ ### References
97
+
98
+ * [ Extending modules] ( /docs/manual/latest/module#extending-modules )
99
+ * [ Extending module signatures] ( /module#extending-module-signatures )
100
+
You can’t perform that action at this time.
0 commit comments