Skip to content

Commit af37d4f

Browse files
authored
Merge pull request #206 from illusionalsagacity/docs-operator-ref-assignment
Syntax lookup: add ref value assignment documentation
2 parents be0bc83 + 0998a56 commit af37d4f

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
---
2+
id: "ref-value-assignment"
3+
keywords: ["ref", "assign"]
4+
name: ":="
5+
summary: "This is the `ref value assignment` operator."
6+
category: "operators"
7+
---
8+
9+
This operator assigns a new value to a `ref`.
10+
11+
<CodeTab labels={["ReScript", "JS Output"]}>
12+
13+
```res
14+
let total = ref(0)
15+
total := 1
16+
```
17+
18+
```js
19+
var total = {
20+
contents: 0
21+
};
22+
23+
total.contents = 1;
24+
```
25+
26+
</CodeTab>
27+
28+
A `ref` is a builtin record type with a `mutable contents` field. Therefore the `:=` operator is just a shorthand version for the following code:
29+
30+
<CodeTab labels={["ReScript", "JS Output"]}>
31+
32+
```res
33+
let total = ref(0)
34+
total.contents = 1
35+
```
36+
37+
```js
38+
var total = {
39+
contents: 0
40+
};
41+
42+
total.contents = 1;
43+
```
44+
45+
</CodeTab>

0 commit comments

Comments
 (0)