Skip to content

Commit 85ad20b

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

File tree

1 file changed

+27
-6
lines changed

1 file changed

+27
-6
lines changed

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

+27-6
Original file line numberDiff line numberDiff line change
@@ -25,27 +25,48 @@ ReScript arrays' items must have the same type, i.e. homogeneous.
2525

2626
### Usage
2727

28-
Access & update an array item like so:
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"
35+
let firstItem = myArray[0] // Some("hello")
3636
37-
myArray[0] = "hey" // now ["hey", "world", "how are you"]
38-
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+
Items in an array can be updated by assigning a value to an index or using a function:
50+
51+
<CodeTab labels={["ReScript", "JS Output"]}>
52+
53+
```res example
54+
let myArray = ["hello", "world", "how are you"]
55+
56+
myArray[0] = "hey" // now ["hey", "world", "how are you"]
57+
58+
myArray->Array.push("?") // ["hey", "world", "how are you", "?"]
59+
60+
myArray->Array.set(0, "bye") // ["bye", "world", "how are you", "?"]
61+
```
62+
```js
63+
var myArray = ["hello", "world", "how are you"];
64+
4665
myArray[0] = "hey";
4766

48-
var pushedValue = myArray.push("bye");
67+
myArray.push("?");
68+
69+
myArray[0] = "bye";
4970
```
5071

5172
</CodeTab>

0 commit comments

Comments
 (0)