Skip to content

update docs for array access to return an option #830

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 33 additions & 7 deletions pages/docs/manual/latest/array-and-list.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,54 @@ var myArray = ["hello", "world", "how are you"];
ReScript arrays' items must have the same type, i.e. homogeneous.

### Usage

Access & update an array item like so:
#### Access
Accessing items in an array will return an `option` and can be done like so:

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

```res example
let myArray = ["hello", "world", "how are you"]

let firstItem = myArray[0] // "hello"

myArray[0] = "hey" // now ["hey", "world", "how are you"]
let firstItem = myArray[0] // Some("hello")

myArray->Array.push("bye")
let tenthItem = myArray->Array.get(10) // None
```
```js
var myArray = ["hello", "world", "how are you"];

var firstItem = myArray[0];

var tenthItem = myArray[10];
```

</CodeTab>

The behavior of returning an `option` is new to V11 when you have [Core](api/core) open.
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.
If you would like to not use an `option`, you can use [`Array.getUnsafe`](api/core/array#value-getUnsafe).

#### Update
Items in an array can be updated by assigning a value to an index or using a function:

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

```res example
let myArray = ["hello", "world", "how are you"]

myArray[0] = "hey" // now ["hey", "world", "how are you"]

myArray->Array.push("?") // ["hey", "world", "how are you", "?"]

myArray->Array.set(0, "bye") // ["bye", "world", "how are you", "?"]
```
```js
var myArray = ["hello", "world", "how are you"];

myArray[0] = "hey";

var pushedValue = myArray.push("bye");
myArray.push("?");

myArray[0] = "bye";
```

</CodeTab>
Expand Down
6 changes: 6 additions & 0 deletions pages/docs/manual/latest/migrate-to-v11.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ Open it so it's available in the global scope.
}
```

One major change to be aware of is that array access now returns an `option`.
```res
let firstItem = myArray[0] // Some("hello")
```
If you would like to not use an `option`, you can use [`Array.getUnsafe`](api/core/array#value-getUnsafe).

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.

See ReScript Core API docs [here](api/core).
Expand Down
2 changes: 1 addition & 1 deletion pages/docs/manual/latest/reserved-keywords.mdx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: "Reserved Keyword"
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The sidebar title didn't match the page title or URL.

title: "Reserved Keywords"
description: "All reserved keywords in ReScript"
canonical: "/docs/manual/latest/reserved-keywords"
---
Expand Down