Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Syntax lookup: Include #316

Merged
merged 5 commits into from
May 5, 2021
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 99 additions & 0 deletions misc_docs/syntax/language_include.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
---
id: "include"
keywords: ["include", "module"]
name: "include"
summary: "This is the `include` keyword."
category: "languageconstructs"
---

Using `include` in a module statically "spreads" another module's content into the module, thus often fulfills the role of "inheritance" or "mixin".
ryyppy marked this conversation as resolved.
Show resolved Hide resolved

ryyppy marked this conversation as resolved.
Show resolved Hide resolved
### 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>

> Note that use of `include` is heavily discouraged and should only be used as a last resort.
ryyppy marked this conversation as resolved.
Show resolved Hide resolved

### References

* [Extending modules](/docs/manual/latest/module#extending-modules)
* [Extending module signatures](/docs/manual/latest/module#extending-module-signatures)