Skip to content

Commit 1c644b6

Browse files
committed
Merge branch 'master' of https://github.com/rescript-lang/rescript-lang.org into vlk-remove-faq-page
2 parents 140bb12 + e66b5d6 commit 1c644b6

11 files changed

+481
-150
lines changed

misc_docs/syntax/language_dict.mdx

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
---
2+
id: "dict"
3+
keywords: ["dict"]
4+
name: "dict"
5+
summary: "This is the `dict{}` syntax"
6+
category: "languageconstructs"
7+
---
8+
9+
> Available in v12+
10+
11+
The `dict{}` syntax is used to represent [dictionaries](dict.md). It's used both when creating dicts, and when pattern matching on dicts.
12+
13+
### Example
14+
15+
<CodeTab labels={["ReScript", "JS Output"]}>
16+
17+
```res
18+
// Create a dict
19+
let d = dict{"A": 5, "B": 6, "C": 7}
20+
21+
// Pattern match on the full dict
22+
let b = switch d {
23+
| dict{"B": b} => Some(b)
24+
| _ => None
25+
}
26+
27+
// Destructure the dict
28+
let dict{"C": ?c} = d
29+
```
30+
31+
```js
32+
let d = {
33+
A: 5,
34+
B: 6,
35+
C: 7
36+
};
37+
38+
let b = d.B;
39+
40+
let b$1 = b !== undefined ? b : undefined;
41+
42+
let c = d.C;
43+
```
44+
45+
</CodeTab>
46+
47+
### References
48+
49+
* [Dictionaries](/docs/manual/latest/dict)
50+
51+

misc_docs/syntax/language_for.mdx

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
---
2+
id: "for"
3+
keywords: ["for", "loop"]
4+
name: "for loop"
5+
summary: "This is the `for` loop."
6+
category: "languageconstructs"
7+
---
8+
9+
ReScript supports `for` loops.
10+
11+
For loops can iterate from a starting value up to (and including) the ending value via the `to` keyword, or in the opposite direction via the `downto` keyword.
12+
13+
### Example
14+
15+
<CodeTab labels={["ReScript", "JS Output"]}>
16+
17+
```res
18+
// Using `to`
19+
for x in 1 to 3 {
20+
Console.log(x)
21+
}
22+
23+
// Using `downto`
24+
for y in 3 downto 1 {
25+
Console.log(y)
26+
}
27+
```
28+
29+
```js
30+
for(var x = 1; x <= 3; ++x){
31+
console.log(x);
32+
}
33+
34+
for(var y = 3; y >= 1; --y){
35+
console.log(y);
36+
}
37+
```
38+
39+
</CodeTab>
40+
41+
42+
### References
43+
44+
* [For Loops](control-flow.md#for-loops)
45+

misc_docs/syntax/language_while.mdx

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
---
2+
id: "while"
3+
keywords: ["while", "loop"]
4+
name: "while loop"
5+
summary: "This is the `while` loop."
6+
category: "languageconstructs"
7+
---
8+
9+
ReScript supports `while` loops. While loops execute its body code block while its condition is true.
10+
11+
ReScript does not have the `break` keyword, but you can easily break out of a while loop by using a mutable binding.
12+
13+
### Example
14+
15+
<CodeTab labels={["ReScript", "JS Output"]}>
16+
17+
```res
18+
let break = ref(false)
19+
20+
while !break.contents {
21+
if Math.random() > 0.3 {
22+
break := true
23+
} else {
24+
Console.log("Still running")
25+
}
26+
}
27+
28+
```
29+
30+
```js
31+
let $$break = {
32+
contents: false
33+
};
34+
35+
while (!$$break.contents) {
36+
if (Math.random() > 0.3) {
37+
$$break.contents = true;
38+
} else {
39+
console.log("Still running");
40+
}
41+
};
42+
```
43+
44+
</CodeTab>
45+
46+
47+
### References
48+
49+
* [While Loops](control-flow.md#while-loops)
50+

pages/docs/manual/v12.0.0/dict.mdx

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
---
2+
title: "Dictionary"
3+
description: "Dictionary data structure in ReScript"
4+
canonical: "/docs/manual/v12.0.0/dict"
5+
---
6+
7+
# Dictionary
8+
9+
ReScript has first class support for dictionaries. Dictionaries are mutable objects with string keys, where all values must have the same type. Dicts compile to regular JavaScript objects at runtime.
10+
11+
## Create
12+
13+
You can create a new dictionary in a few different ways, depending on your use case.
14+
15+
<CodeTab labels={["ReScript", "JS Output"]}>
16+
17+
```res prelude
18+
// Using the first class dict syntax
19+
let d = dict{"A": 5, "B": 6}
20+
21+
// Programatically via the standard library
22+
let d2 = Dict.fromArray([("A", 5), ("B", 6)])
23+
```
24+
25+
```js
26+
let d = {
27+
A: 5,
28+
B: 6
29+
};
30+
31+
let d2 = Object.fromEntries([
32+
[
33+
"A",
34+
5
35+
],
36+
[
37+
"B",
38+
6
39+
]
40+
]);
41+
42+
```
43+
44+
</CodeTab>
45+
46+
A few things to note here:
47+
48+
* Using the first class `dict{}` syntax compiles cleanly to a JavaScript object directly
49+
* Using `Dict.fromArray` is useful when you need to create a dictionary programatically
50+
51+
## Access
52+
53+
You can access values from a Dictionary either via the the standard library `Dict` module functions, or using pattern matching.
54+
55+
<CodeTab labels={["ReScript", "JS Output"]}>
56+
57+
```res prelude
58+
let d = dict{"A": 5, "B": 6, "C": 7}
59+
60+
// Using `Dict.get`
61+
let a = d->Dict.get("A")
62+
63+
// Switching on the full dict
64+
let b = switch d {
65+
| dict{"B": b} => Some(b)
66+
| _ => None
67+
}
68+
69+
// Destructuring
70+
let dict{"C": ?c} = d
71+
```
72+
73+
```js
74+
let d = {
75+
A: 5,
76+
B: 6,
77+
C: 7
78+
};
79+
80+
let a = d["A"];
81+
82+
let b = d.B;
83+
84+
let b$1 = b !== undefined ? b : undefined;
85+
86+
let c = d.C;
87+
```
88+
</CodeTab>
89+
90+
> In the Destructuring example, we're using the `?` optional pattern match syntax to pull out the `C` key value as an optional, regardless of if the dict has it or not.
91+
92+
## Pattern matching
93+
Dictionaries have first class support for pattern matching. Read more in the [dedicated guide on pattern matching and destructring in ReScript](pattern-matching-destructuring.md#match-on-dictionaries).
94+
95+
## Updating and setting values
96+
97+
You can set and update new values on your dictionary using the `Dict.set` function. All updates are mutable.
98+
99+
<CodeTab labels={["ReScript", "JS Output"]}>
100+
101+
```res prelude
102+
let d = dict{"A": 5, "B": 6}
103+
104+
d->Dict.set("C", 7)
105+
```
106+
107+
```js
108+
let d = {
109+
A: 5,
110+
B: 6
111+
};
112+
113+
d["C"] = 7;
114+
```
115+
116+
</CodeTab>
117+
118+
## Advanced example: Pattern matching on JSON
119+
120+
JSON objects are represented as dictionaries (`dict<JSON.t>`). You can leverage that fact to decode JSON in a nice way, using only language features:
121+
122+
<CodeTab labels={["ReScript", "JS Output"]}>
123+
124+
```res prelude
125+
type user = {
126+
name: string,
127+
email: string,
128+
}
129+
130+
/** Decode JSON to a `user`. */
131+
let decodeUser = (json: JSON.t) => {
132+
switch json {
133+
| Object(dict{"name": JSON.String(name), "email": JSON.String(email)}) =>
134+
Some({name, email})
135+
| _ => None
136+
}
137+
}
138+
139+
```
140+
141+
```js
142+
function decodeUser(json) {
143+
if (typeof json !== "object" || json === null || Array.isArray(json)) {
144+
return;
145+
}
146+
let name = json.name;
147+
if (typeof name !== "string") {
148+
return;
149+
}
150+
let email = json.email;
151+
if (typeof email === "string") {
152+
return {
153+
name: name,
154+
email: email
155+
};
156+
}
157+
158+
}
159+
```
160+
161+
</CodeTab>

0 commit comments

Comments
 (0)