-
-
Notifications
You must be signed in to change notification settings - Fork 248
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #316 from kevanstannard/syntax-lookup-include
Syntax lookup: Include
- Loading branch information
Showing
1 changed file
with
100 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
--- | ||
id: "include" | ||
keywords: ["include", "module"] | ||
name: "include" | ||
summary: "This is the `include` keyword." | ||
category: "languageconstructs" | ||
--- | ||
|
||
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. | ||
|
||
> Note that `include` may make your code more complex and harder to understand when abused. Think twice before using it. | ||
### Include module example | ||
|
||
<CodeTab labels={["ReScript", "JS Output"]}> | ||
|
||
```res | ||
module Message = { | ||
let greeting = "Hello" | ||
} | ||
module Greeter = { | ||
include Message | ||
let greet = name => greeting ++ " " ++ name | ||
} | ||
``` | ||
|
||
```js | ||
var greeting = "Hello"; | ||
|
||
var Message = { | ||
greeting: greeting, | ||
}; | ||
|
||
function greet(name) { | ||
return "Hello " + name; | ||
} | ||
|
||
var Greeter = { | ||
greeting: greeting, | ||
greet: greet, | ||
}; | ||
``` | ||
|
||
</CodeTab> | ||
|
||
Similarly, module signatures can also be extended by other module signatures. | ||
|
||
### Include signature example | ||
|
||
<CodeTab labels={["ReScript", "JS Output"]}> | ||
|
||
```res | ||
module type Message = { | ||
let greeting: string | ||
} | ||
module type Greeter = { | ||
include Message | ||
let greet: string => string | ||
} | ||
``` | ||
|
||
```js | ||
// No output | ||
``` | ||
|
||
</CodeTab> | ||
|
||
Lastly, you can also extract module types from existing modules using `module type of`. | ||
|
||
### Include "module type of" example | ||
|
||
<CodeTab labels={["ReScript", "JS Output"]}> | ||
|
||
```res | ||
module Message = { | ||
let greeting = "Hello" | ||
} | ||
module type Greeter = { | ||
// Includes the type definitions of Message | ||
include module type of Message | ||
let greet: string => string | ||
} | ||
``` | ||
|
||
```js | ||
var Message = { | ||
greeting: "Hello" | ||
}; | ||
``` | ||
|
||
</CodeTab> | ||
|
||
|
||
### References | ||
|
||
* [Extending modules](/docs/manual/latest/module#extending-modules) | ||
* [Extending module signatures](/docs/manual/latest/module#extending-module-signatures) |