Skip to content

Commit

Permalink
Merge pull request #316 from kevanstannard/syntax-lookup-include
Browse files Browse the repository at this point in the history
Syntax lookup: Include
  • Loading branch information
ryyppy authored May 5, 2021
2 parents b8c596d + a7a9755 commit ae5d3a8
Showing 1 changed file with 100 additions and 0 deletions.
100 changes: 100 additions & 0 deletions misc_docs/syntax/language_include.mdx
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)

0 comments on commit ae5d3a8

Please sign in to comment.