|
| 1 | +--- |
| 2 | +title: "Dictionary" |
| 3 | +description: "Dictionary type from ReScript Core" |
| 4 | +canonical: "/docs/manual/v12.0.0/dict" |
| 5 | +--- |
| 6 | + |
| 7 | +# Dictionary |
| 8 | + |
| 9 | +A mutable dictionary with string keys. |
| 10 | +Compiles to a regular JavaScript object. |
| 11 | +Defined in the [Core](/docs/manual/v12.0.0/api/core/dict). |
| 12 | + |
| 13 | +## Create |
| 14 | + |
| 15 | +We have a dedicated syntax to create a new Dictionary. |
| 16 | + |
| 17 | +<CodeTab labels={["ReScript", "JS Output"]}> |
| 18 | + |
| 19 | +```res prelude |
| 20 | +let d = dict{"A": 5, "B": 6} |
| 21 | +``` |
| 22 | + |
| 23 | +```js |
| 24 | +let d = { |
| 25 | + A: 5, |
| 26 | + B: 6 |
| 27 | +}; |
| 28 | +``` |
| 29 | + |
| 30 | +</CodeTab> |
| 31 | + |
| 32 | +⚠️ The keys of a dictionary are always strings and the values all have the same type. |
| 33 | +You will get a compiler error if this is not the case! |
| 34 | + |
| 35 | +<CodeTab labels={["ReScript", "JS Output"]}> |
| 36 | + |
| 37 | +```res prelude |
| 38 | +let d = dict{"A": 5, "B": "Hej"} |
| 39 | +``` |
| 40 | + |
| 41 | +```js |
| 42 | +We've found a bug for you! |
| 43 | +
|
| 44 | + 1 │ let d = dict{"A": 5, "B": "Hej"} |
| 45 | +
|
| 46 | + This has type: string |
| 47 | + But it's expected to have type: int |
| 48 | + |
| 49 | + You can convert string to int with Int.fromString. |
| 50 | +``` |
| 51 | + |
| 52 | +</CodeTab> |
| 53 | + |
| 54 | +## Access |
| 55 | + |
| 56 | +You can access values from a Dictionary either via the the Core module functions, |
| 57 | +or using pattern matching. |
| 58 | + |
| 59 | +<CodeTab labels={["ReScript", "JS Output"]}> |
| 60 | + |
| 61 | +```res prelude |
| 62 | +let d = dict{"A": 5, "B": 6} |
| 63 | +let a : option<int> = d->Dict.get("A") |
| 64 | +
|
| 65 | +let b = switch d { |
| 66 | +| dict{"B": b} => Some(b) |
| 67 | +| _ => None |
| 68 | +} |
| 69 | +``` |
| 70 | + |
| 71 | +```js |
| 72 | +let d = { |
| 73 | + A: 5, |
| 74 | + B: 6 |
| 75 | +}; |
| 76 | + |
| 77 | +let a = d["A"]; |
| 78 | + |
| 79 | +let b = d.B; |
| 80 | + |
| 81 | +let b$1 = b !== undefined ? b : undefined; |
| 82 | +``` |
| 83 | + |
| 84 | +</CodeTab> |
| 85 | + |
| 86 | +### Pattern match with JSON.t |
| 87 | + |
| 88 | +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. |
| 89 | + |
| 90 | +<CodeTab labels={["ReScript", "JS Output"]}> |
| 91 | + |
| 92 | +```res prelude |
| 93 | +@module("some-module") |
| 94 | +external getSettings: string => JSON.t = "getSettings" |
| 95 | +
|
| 96 | +let vapidKey = switch getSettings("user") { |
| 97 | +| JSON.Object(dict{ |
| 98 | + "notifications": // A nested object structure |
| 99 | + JSON.Object(dict{"vapidKey": JSON.String(key)}), |
| 100 | + }) => |
| 101 | + Some(key) |
| 102 | +| _ => { |
| 103 | + Console.log("key not found") |
| 104 | + None |
| 105 | + } |
| 106 | +} |
| 107 | +``` |
| 108 | + |
| 109 | +```js |
| 110 | +import * as SomeModule from "some-module"; |
| 111 | + |
| 112 | +let match = SomeModule.getSettings("user"); |
| 113 | + |
| 114 | +let vapidKey; |
| 115 | + |
| 116 | +if (typeof match === "object" && match !== null && !Array.isArray(match)) { |
| 117 | + let match$1 = match.notifications; |
| 118 | + if (typeof match$1 === "object" && match$1 !== null && !Array.isArray(match$1)) { |
| 119 | + let key = match$1.vapidKey; |
| 120 | + if (typeof key === "string") { |
| 121 | + vapidKey = key; |
| 122 | + } else { |
| 123 | + console.log("key not found"); |
| 124 | + vapidKey = undefined; |
| 125 | + } |
| 126 | + } else { |
| 127 | + console.log("key not found"); |
| 128 | + vapidKey = undefined; |
| 129 | + } |
| 130 | +} else { |
| 131 | + console.log("key not found"); |
| 132 | + vapidKey = undefined; |
| 133 | +} |
| 134 | +``` |
| 135 | + |
| 136 | +</CodeTab> |
| 137 | + |
| 138 | +## Mutable Update |
| 139 | + |
| 140 | +Updating an entry happens via the `Dict.set` function. |
| 141 | + |
| 142 | +<CodeTab labels={["ReScript", "JS Output"]}> |
| 143 | + |
| 144 | +```res prelude |
| 145 | +let d = dict{"A": 5, "B": 6} |
| 146 | +let a : option<int> = d->Dict.get("A") |
| 147 | +
|
| 148 | +let b = switch d { |
| 149 | +| dict{"B": b} => Some(b) |
| 150 | +| _ => None |
| 151 | +} |
| 152 | +``` |
| 153 | + |
| 154 | +```js |
| 155 | +let d = { |
| 156 | + A: 5, |
| 157 | + B: 6 |
| 158 | +}; |
| 159 | + |
| 160 | +d["A"] = -1; |
| 161 | + |
| 162 | +d["C"] = 0; |
| 163 | +``` |
| 164 | + |
| 165 | +</CodeTab> |
0 commit comments