Skip to content

Commit 303ce37

Browse files
committed
update docs for array access to return an option
1 parent df3aaec commit 303ce37

File tree

3 files changed

+40
-8
lines changed

3 files changed

+40
-8
lines changed

pages/docs/manual/latest/array-and-list.mdx

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,28 +24,54 @@ var myArray = ["hello", "world", "how are you"];
2424
ReScript arrays' items must have the same type, i.e. homogeneous.
2525

2626
### Usage
27-
28-
Access & update an array item like so:
27+
#### Access
28+
Accessing items in an array will return an `option` and can be done like so:
2929

3030
<CodeTab labels={["ReScript", "JS Output"]}>
3131

3232
```res example
3333
let myArray = ["hello", "world", "how are you"]
3434
35-
let firstItem = myArray[0] // "hello"
36-
37-
myArray[0] = "hey" // now ["hey", "world", "how are you"]
35+
let firstItem = myArray[0] // Some("hello")
3836
39-
myArray->Array.push("bye")
37+
let tenthItem = myArray->Array.get(10) // None
4038
```
4139
```js
4240
var myArray = ["hello", "world", "how are you"];
4341

4442
var firstItem = myArray[0];
4543

44+
var tenthItem = myArray[10];
45+
```
46+
47+
</CodeTab>
48+
49+
The behavior of returning an `option` is new to V11 when you have [Core](api/core) open.
50+
It provides a safer way to access array items, which is especially useful when you're not sure if the index is out of bounds.
51+
If you would like to not use an `option`, you can use [`Array.getUnsafe`](api/core/array#value-getUnsafe).
52+
53+
#### Update
54+
Items in an array can be updated by assigning a value to an index or using a function:
55+
56+
<CodeTab labels={["ReScript", "JS Output"]}>
57+
58+
```res example
59+
let myArray = ["hello", "world", "how are you"]
60+
61+
myArray[0] = "hey" // now ["hey", "world", "how are you"]
62+
63+
myArray->Array.push("?") // ["hey", "world", "how are you", "?"]
64+
65+
myArray->Array.set(0, "bye") // ["bye", "world", "how are you", "?"]
66+
```
67+
```js
68+
var myArray = ["hello", "world", "how are you"];
69+
4670
myArray[0] = "hey";
4771

48-
var pushedValue = myArray.push("bye");
72+
myArray.push("?");
73+
74+
myArray[0] = "bye";
4975
```
5076

5177
</CodeTab>

pages/docs/manual/latest/migrate-to-v11.mdx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ Open it so it's available in the global scope.
6262
}
6363
```
6464

65+
One major change to be aware of is that array access now returns an `option`.
66+
```res
67+
let firstItem = myArray[0] // Some("hello")
68+
```
69+
If you would like to not use an `option`, you can use [`Array.getUnsafe`](api/core/array#value-getUnsafe).
70+
6571
For a detailed explanation on migration to ReScript Core, please refer to its [migration guide](https://github.com/rescript-association/rescript-core#migration). A semi-automated script is available as well.
6672

6773
See ReScript Core API docs [here](api/core).

pages/docs/manual/latest/reserved-keywords.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: "Reserved Keyword"
2+
title: "Reserved Keywords"
33
description: "All reserved keywords in ReScript"
44
canonical: "/docs/manual/latest/reserved-keywords"
55
---

0 commit comments

Comments
 (0)