-
Notifications
You must be signed in to change notification settings - Fork 3
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 #14 from rescript-lang/extract-event-target
Extract reusable functions in inheritance
- Loading branch information
Showing
268 changed files
with
2,186 additions
and
52,509 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
6 changes: 3 additions & 3 deletions
6
...nt/docs/contributing/module-structure.mdx → ...ocs/contributing/api-module-structure.mdx
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
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
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
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,86 @@ | ||
--- | ||
title: Module Type Structure | ||
description: Learn more about the module structure of @rescript/webapi. | ||
slug: "03-module-type-structure" | ||
--- | ||
|
||
import { Aside, FileTree, Code } from "@astrojs/starlight/components"; | ||
|
||
Every interface in a Web API module can potentially contain methods. These methods are modeled in a separate module named after the interface. | ||
|
||
The primary reason for this separation is to handle method overloads. | ||
As explained in the [Design Philosophy](../design-philosophy) section, ReScript does not permit records to define the same properties more than once. | ||
Therefore, methods with overloads cannot be modeled within the same record type. | ||
|
||
## Bindings | ||
|
||
Another advantage of having a separate file is that these bindings can utilize all types defined in the API module. | ||
Under normal circumstances, the type module only contains `@send` bindings where the type is the first parameter. | ||
|
||
<FileTree> | ||
|
||
- DOMAPI | ||
- HTMLButtonElement.res | ||
|
||
</FileTree> | ||
|
||
```ReScript | ||
/** | ||
Returns whether a form will validate when it is submitted, without having to submit it. | ||
[Read more on MDN]( | ||
https://developer.mozilla.org/docs/Web/API/HTMLButtonElement/checkValidity) | ||
*/ | ||
@send | ||
external checkValidity: htmlButtonElement => bool = "checkValidity" | ||
``` | ||
|
||
## Inheritance | ||
|
||
When an interface inherits from another interface, the base interface methods can be [included](https://rescript-lang.org/syntax-lookup#include) into the inheriting interface. | ||
All methods from [HTMLElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement#instance_methods) should also be available on [HTMLButtonElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLButtonElement#instance_methods). | ||
|
||
export const htmlElementModule = ` | ||
open DOMAPI | ||
// A concrete type for \`T.t\` is passed later using the \`include\` keyword. | ||
module Impl = (T: { type t }) => { | ||
/** | ||
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/HTMLElement/focus) | ||
*/ | ||
@send | ||
external focus: (T.t, ~options: focusOptions=?) => unit = "focus" | ||
} | ||
include Impl({ type t = htmlElement }) | ||
`; | ||
|
||
<Code | ||
code={htmlElementModule} | ||
title="DOMAPI/HTMLElement.res" | ||
lang="ReScript" | ||
></Code> | ||
|
||
export const buttonModule = ` | ||
open DOMAPI | ||
// Include all the methods from HTMLElement | ||
include HTMLElement.Impl({ type t = htmlButtonElement }) | ||
// Add additional methods specific to HTMLButtonElement: | ||
/** | ||
Returns whether a form will validate when it is submitted, without having to submit it. | ||
[Read more on MDN]( | ||
https://developer.mozilla.org/docs/Web/API/HTMLButtonElement/checkValidity) | ||
*/ | ||
@send | ||
external checkValidity: htmlButtonElement => bool = "checkValidity" | ||
`; | ||
|
||
<Code | ||
code={buttonModule} | ||
title="DOMAPI/HTMLButtonElement.res" | ||
lang="ReScript" | ||
></Code> |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.