Skip to content

Commit 15a0cc4

Browse files
Syntax lookup overhaul + add missing constructs
1 parent 98935fa commit 15a0cc4

63 files changed

Lines changed: 1358 additions & 80 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

markdown-pages/docs/manual/overview.mdx

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,13 @@ order: 1
5757

5858
### Number
5959

60-
| JavaScript | ReScript |
61-
| ----------- | ------------ |
62-
| `3` | Same \* |
63-
| `3.1415` | Same |
64-
| `3 + 4` | Same |
65-
| `3.0 + 4.5` | `3.0 +. 4.5` |
66-
| `5 % 3` | `mod(5, 3)` |
60+
| JavaScript | ReScript |
61+
| ----------- | -------- |
62+
| `3` | Same \* |
63+
| `3.1415` | Same |
64+
| `3 + 4` | Same |
65+
| `3.0 + 4.5` | Same |
66+
| `5 % 3` | Same |
6767

6868
\* JS has no distinction between integer and float.
6969

@@ -235,7 +235,7 @@ The last expression of a block delimited by `{}` implicitly returns (including f
235235
| ------------------------------- | ------------------------------------ | ------------------------------------------ |
236236
| String | `"Hello"` | `"Hello"` |
237237
| String Interpolation | `` `Hello ${message}` `` | `"Hello " + message` |
238-
| Character (disrecommended) | `'x'` | `120` (char code) |
238+
| Character (discouraged) | `'x'` | `120` (char code) |
239239
| Integer | `23`, `-23` | `23`, `-23` |
240240
| Float | `23.0`, `-23.0` | `23.0`, `-23.0` |
241241
| Integer Addition | `23 + 1` | `23 + 1` |
@@ -247,7 +247,7 @@ The last expression of a block delimited by `{}` implicitly returns (including f
247247
| Comparison | `>`, `<`, `>=`, `<=` | `>`, `<`, `>=`, `<=` |
248248
| Boolean operation | `!`, `&&`, `\|\|` | `!`, `&&`, `\|\|` |
249249
| Shallow and deep Equality | `===`, `==` | `===`, `==` |
250-
| List (disrecommended) | `list{1, 2, 3}` | `{hd: 1, tl: {hd: 2, tl: {hd: 3, tl: 0}}}` |
250+
| List (discouraged) | `list{1, 2, 3}` | `{hd: 1, tl: {hd: 2, tl: {hd: 3, tl: 0}}}` |
251251
| List Prepend | `list{a1, a2, ...oldList}` | `{hd: a1, tl: {hd: a2, tl: theRest}}` |
252252
| Array | `[1, 2, 3]` | `[1, 2, 3]` |
253253
| Record | `type t = {b: int}; let a = {b: 10}` | `var a = {b: 10}` |

markdown-pages/docs/manual/primitive-types.mdx

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,12 @@ ReScript's `true/false` compiles into a JavaScript `true/false`.
148148

149149
32-bits, truncated when necessary. We provide the usual operations on them: `+`, `-`, `*`, `/`, etc. See [Int](/docs/manual/api/stdlib/int) for helper functions.
150150

151+
Integer operators include `+`, `-`, `*`, `/`, `**`, and `%`.
152+
153+
`%` keeps the familiar `mod` naming, but its semantics are remainder (same behavior as JavaScript `%`).
154+
155+
Bitwise operators for `int`: `~~~`, `&&&`, `|||`, `^^^`, `<<`, `>>`, `>>>`.
156+
151157
**Be careful when you bind to JavaScript numbers!** Since ReScript integers have a much smaller range than JavaScript numbers, data might get lost when dealing with large numbers. In those cases it’s much safer to bind the numbers as **float**. Be extra mindful of this when binding to JavaScript Dates and their epoch time.
152158

153159
To improve readability, you may place underscores in the middle of numeric literals such as `1_000_000`. Note that underscores can be placed anywhere within a number, not just every three digits.
@@ -156,6 +162,8 @@ To improve readability, you may place underscores in the middle of numeric liter
156162

157163
Float requires other operators: `+.`, `-.`, `*.`, `/.`, etc. Like `0.5 +. 0.6`. See [Float](/docs/manual/api/stdlib/float) for helper functions.
158164

165+
Float also supports `**` (exponentiation) and `%` (remainder semantics).
166+
159167
As with integers, you may use underscores within literals to improve readability.
160168

161169
### Int-to-Float Coercion
@@ -179,7 +187,7 @@ var result = 1 + 2;
179187
**Since 11.1**
180188

181189
For values which are too large to be represented by Int or Float, there is the `bigint` primitive type.
182-
We provide the usual operations on them: `+`, `-`, `*`, `/`, etc. See [BigInt](/docs/manual/api/stdlib/bigint) for helper functions.
190+
We provide the usual operations on them: `+`, `-`, `*`, `/`, `**`, `%`, etc. See [BigInt](/docs/manual/api/stdlib/bigint) for helper functions.
183191

184192
A `bigint` number is denoted by a trailing `n` like so: `42n`.
185193

@@ -209,28 +217,26 @@ It also supports all the bitwise operations, except unsigned shift right (`>>>`)
209217
```res example
210218
open! BigInt
211219
212-
let a = land(1n, 1n)
213-
let b = lor(1n, 1n)
214-
let c = lxor(1n, 1n)
215-
let d = lnot(1n)
216-
let e = lsl(1n, 1n)
217-
let f = asr(1n, 1n)
220+
let a = 5n &&& 3n
221+
let b = 5n ||| 2n
222+
let c = 5n ^^^ 1n
223+
let d = ~~~5n
224+
let e = 5n << 1n
225+
let f = 5n >> 1n
218226
```
219227

220228
```js
221-
var Js_bigint = require("./stdlib/js_bigint.js");
222-
223-
var a = 1n & 1n;
229+
var a = 5n & 3n;
224230

225-
var b = 1n | 1n;
231+
var b = 5n | 2n;
226232

227-
var c = 1n ^ 1n;
233+
var c = 5n ^ 1n;
228234

229-
var d = Js_bigint.lnot(1n);
235+
var d = ~5n;
230236

231-
var e = 1n << 1n;
237+
var e = 5n << 1n;
232238

233-
var f = 1n >> 1n;
239+
var f = 5n >> 1n;
234240
```
235241

236242
</CodeTab>
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
---
2+
id: "and"
3+
keywords: ["and", "let rec and", "type rec and", "mutual recursion"]
4+
name: "and"
5+
summary: "This is the `and` keyword for mutually recursive definitions."
6+
category: "languageconstructs"
7+
---
8+
9+
Use `and` to define multiple items in one mutually-recursive group.
10+
11+
### Example 1: `let rec ... and ...`
12+
13+
<CodeTab labels={["ReScript", "JS Output"]}>
14+
15+
```res
16+
let rec isEven = n => n == 0 || isOdd(n - 1)
17+
and isOdd = n => n != 0 && isEven(n - 1)
18+
```
19+
20+
```js
21+
function isEven(n) {
22+
return n === 0 || isOdd((n - 1) | 0);
23+
}
24+
25+
function isOdd(n) {
26+
return n !== 0 && isEven((n - 1) | 0);
27+
}
28+
```
29+
30+
</CodeTab>
31+
32+
### Example 2: `type rec ... and ...`
33+
34+
<CodeTab labels={["ReScript", "JS Output"]}>
35+
36+
```res
37+
type rec expr = Add(expr, term) | Term(term)
38+
and term = Int(int)
39+
```
40+
41+
```js
42+
// Type declarations are erased from JavaScript output.
43+
```
44+
45+
</CodeTab>
46+
47+
### References
48+
49+
- [Recursive Functions](../docs/manual/function.mdx#recursive-functions)
50+
- [Type](../docs/manual/type.mdx)

markdown-pages/syntax-lookup/language_async.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ id: "async"
33
keywords: ["async"]
44
name: "async"
55
summary: "This is the `async` keyword."
6-
category: "languageconstructs"
6+
category: "languagecontrolflow"
77
---
88

99
**Since 10.1**
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
---
2+
id: "attached-doc-comment"
3+
keywords: ["doc comment", "attached doc comment", "/**"]
4+
name: "/** ... */"
5+
summary: "This is the `attached doc comment` syntax."
6+
category: "languagecomments"
7+
---
8+
9+
Use an attached doc comment directly before a value or type declaration.
10+
11+
### Example
12+
13+
<CodeTab labels={["ReScript", "JS Output"]}>
14+
15+
```res
16+
/** Returns the full name. */
17+
let fullName = (first, last) => first ++ " " ++ last
18+
```
19+
20+
```js
21+
function fullName(first, last) {
22+
return first + " " + last;
23+
}
24+
```
25+
26+
</CodeTab>
27+
28+
### References
29+
30+
- [Overview](../docs/manual/overview.mdx#comments)

markdown-pages/syntax-lookup/language_await.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ id: "await"
33
keywords: ["await"]
44
name: "await"
55
summary: "This is the `await` keyword."
6-
category: "languageconstructs"
6+
category: "languagecontrolflow"
77
---
88

99
**Since 10.1**
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
---
2+
id: "block-comment"
3+
keywords: ["comment", "block comment", "/*", "*/"]
4+
name: "/* ... */"
5+
summary: "This is the `block comment` syntax."
6+
category: "languagecomments"
7+
---
8+
9+
Use `/* ... */` for multi-line comments.
10+
11+
### Example
12+
13+
<CodeTab labels={["ReScript", "JS Output"]}>
14+
15+
```res
16+
/*
17+
This is a block comment.
18+
*/
19+
let greeting = "hello"
20+
```
21+
22+
```js
23+
var greeting = "hello";
24+
```
25+
26+
</CodeTab>
27+
28+
### References
29+
30+
- [Overview](../docs/manual/overview.mdx#comments)

markdown-pages/syntax-lookup/language_char_literal.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ id: "char-literal"
33
keywords: ["char"]
44
name: "' '"
55
summary: "This is the `char` literal syntax."
6-
category: "languageconstructs"
6+
category: "languagepatternsvalues"
77
---
88

99
A `char` literal is composed of two **single** quotes. Double quotes are reserved for the `string` type. Note that `char` is essentially an integer in JS since version 10.0.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
---
2+
id: "covariant-type-parameter"
3+
keywords: ["<+a>", "+'a", "covariant", "variance", "type parameter"]
4+
name: "<+a>"
5+
summary: "This is a `covariant type parameter` annotation."
6+
category: "languagetypes"
7+
---
8+
9+
Use a `+` variance annotation on a type parameter to mark it as covariant.
10+
11+
### Example
12+
13+
<CodeTab labels={["ReScript", "JS Output"]}>
14+
15+
```res
16+
type source<+'a> = unit => 'a
17+
18+
let fromValue = (x: 'a): source<'a> => () => x
19+
```
20+
21+
```js
22+
function fromValue(x) {
23+
return (_) => x;
24+
}
25+
```
26+
27+
</CodeTab>
28+
29+
### References
30+
31+
- [Type Parameters](../docs/manual/type.mdx#type-parameter-aka-generic)

markdown-pages/syntax-lookup/language_dict.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ id: "dict"
33
keywords: ["dict"]
44
name: "dict"
55
summary: "This is the `dict{}` syntax"
6-
category: "languageconstructs"
6+
category: "languagetypes"
77
---
88

99
> Available in v12+

0 commit comments

Comments
 (0)