-
-
Notifications
You must be signed in to change notification settings - Fork 251
Initial dictionary page #1006
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
Initial dictionary page #1006
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ | |
"tuple", | ||
"record", | ||
"object", | ||
"dict", | ||
"variant", | ||
"polymorphic-variant", | ||
"null-undefined-option", | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
--- | ||
title: "Dictionary" | ||
description: "Dictionary type from ReScript Core" | ||
canonical: "/docs/manual/v12.0.0/dict" | ||
--- | ||
|
||
# Dictionary | ||
|
||
A mutable dictionary with string keys. | ||
Compiles to a regular JavaScript object. | ||
Defined in the [Core](/docs/manual/v12.0.0/api/core/dict). | ||
|
||
## Create | ||
|
||
We have a dedicated syntax to create a new Dictionary. | ||
|
||
<CodeTab labels={["ReScript", "JS Output"]}> | ||
|
||
```res prelude | ||
let d = dict{"A": 5, "B": 6} | ||
``` | ||
|
||
```js | ||
let d = { | ||
A: 5, | ||
B: 6 | ||
}; | ||
``` | ||
|
||
</CodeTab> | ||
|
||
⚠️ The keys of a dictionary are always strings and the values all have the same type. | ||
You will get a compiler error if this is not the case! | ||
|
||
<CodeTab labels={["ReScript", "JS Output"]}> | ||
|
||
```res prelude | ||
let d = dict{"A": 5, "B": "Hej"} | ||
``` | ||
|
||
```js | ||
zth marked this conversation as resolved.
Show resolved
Hide resolved
|
||
We've found a bug for you! | ||
|
||
1 │ let d = dict{"A": 5, "B": "Hej"} | ||
|
||
This has type: string | ||
But it's expected to have type: int | ||
|
||
You can convert string to int with Int.fromString. | ||
``` | ||
|
||
</CodeTab> | ||
|
||
## Access | ||
|
||
You can access values from a Dictionary either via the the Core module functions, | ||
or using pattern matching. | ||
|
||
<CodeTab labels={["ReScript", "JS Output"]}> | ||
|
||
```res prelude | ||
let d = dict{"A": 5, "B": 6} | ||
let a : option<int> = d->Dict.get("A") | ||
|
||
let b = switch d { | ||
| dict{"B": b} => Some(b) | ||
| _ => None | ||
} | ||
``` | ||
|
||
```js | ||
let d = { | ||
A: 5, | ||
B: 6 | ||
}; | ||
|
||
let a = d["A"]; | ||
|
||
let b = d.B; | ||
|
||
let b$1 = b !== undefined ? b : undefined; | ||
``` | ||
|
||
</CodeTab> | ||
|
||
### Pattern match with JSON.t | ||
|
||
Pattern matching a Dictionary with the `dict{}` can be very elegant if you received an (external) [JSON.t](/docs/manual/v12.0.0/api/core/json) object. | ||
|
||
<CodeTab labels={["ReScript", "JS Output"]}> | ||
|
||
```res prelude | ||
@module("some-module") | ||
external getSettings: string => JSON.t = "getSettings" | ||
|
||
let vapidKey = switch getSettings("user") { | ||
| JSON.Object(dict{ | ||
"notifications": // A nested object structure | ||
JSON.Object(dict{"vapidKey": JSON.String(key)}), | ||
}) => | ||
Some(key) | ||
| _ => { | ||
Console.log("key not found") | ||
None | ||
} | ||
} | ||
``` | ||
|
||
```js | ||
import * as SomeModule from "some-module"; | ||
|
||
let match = SomeModule.getSettings("user"); | ||
|
||
let vapidKey; | ||
|
||
if (typeof match === "object" && match !== null && !Array.isArray(match)) { | ||
let match$1 = match.notifications; | ||
if (typeof match$1 === "object" && match$1 !== null && !Array.isArray(match$1)) { | ||
let key = match$1.vapidKey; | ||
if (typeof key === "string") { | ||
vapidKey = key; | ||
} else { | ||
console.log("key not found"); | ||
vapidKey = undefined; | ||
} | ||
} else { | ||
console.log("key not found"); | ||
vapidKey = undefined; | ||
} | ||
} else { | ||
console.log("key not found"); | ||
vapidKey = undefined; | ||
} | ||
``` | ||
|
||
</CodeTab> | ||
|
||
## Mutable Update | ||
|
||
Updating an entry happens via the `Dict.set` function. | ||
|
||
<CodeTab labels={["ReScript", "JS Output"]}> | ||
|
||
```res prelude | ||
let d = dict{"A": 5, "B": 6} | ||
let a : option<int> = d->Dict.get("A") | ||
|
||
let b = switch d { | ||
| dict{"B": b} => Some(b) | ||
| _ => None | ||
} | ||
``` | ||
|
||
```js | ||
let d = { | ||
A: 5, | ||
B: 6 | ||
}; | ||
|
||
d["A"] = -1; | ||
|
||
d["C"] = 0; | ||
``` | ||
|
||
</CodeTab> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.