rl_lang/docs/entries/concepts/maps/
mod.rs1use crate::docs::entry::{ConceptCategory, ConceptEntry, DescriptionEntry, DescriptionKind};
2
3pub static MAPS: ConceptEntry = ConceptEntry {
4 name: "maps",
5 summary: "",
6 category: ConceptCategory::Syntax,
7 prerequisites: &["arrays"],
8 descriptions: &[
9 DescriptionEntry {
10 kind: DescriptionKind::Explanation,
11 title: None,
12 description: "declare a mutable map with `dec map[<key type>, <value type>] <n> = {<key>: <value>, ...}`",
13 examples: &[
14 "dec map[string, int] scores = {\"alice\": 95, \"bob\": 82}",
15 "dec map[int, string] names = {1: \"one\", 2: \"two\"}",
16 "dec map[string, bool] flags = {\"dark_mode\": true}",
17 ],
18 expected_output: &[],
19 },
20 DescriptionEntry {
21 kind: DescriptionKind::Explanation,
22 title: None,
23 description: "read a value by key with `map[key]`",
24 examples: &[
25 "dec map[string, int] scores = {\"alice\": 95, \"bob\": 82}\nprintln(scores[\"alice\"]) // 95",
26 ],
27 expected_output: &[],
28 },
29 DescriptionEntry {
30 kind: DescriptionKind::Explanation,
31 title: None,
32 description: "write or update a value by key with `map[key] = value` - this both inserts new keys and overwrites existing ones",
33 examples: &[
34 "dec map[string, int] scores = {\"alice\": 95}\nscores[\"bob\"] = 82 // insert\nscores[\"alice\"] = 100 // overwrite",
35 ],
36 expected_output: &[],
37 },
38 DescriptionEntry {
39 kind: DescriptionKind::Explanation,
40 title: None,
41 description: "maps are typed like arrays: every key must share one type, and every value must share one type",
42 examples: &[
43 "dec map[string, int] scores = {\"alice\": 95}\n// scores[\"bob\"] = \"not a number\" // error: type mismatch",
44 ],
45 expected_output: &[],
46 },
47 DescriptionEntry {
48 kind: DescriptionKind::Explanation,
49 title: None,
50 description: "only int, string, bool, byte, and char can be used as key types - float keys are rejected, since floating point equality is unreliable for lookup",
51 examples: &[
52 "// dec map[float, int] bad = {1.5: 1} // error: float cannot be used as a map key",
53 ],
54 expected_output: &[],
55 },
56 DescriptionEntry {
57 kind: DescriptionKind::Explanation,
58 title: None,
59 description: "map values can be any type, including arrays or other maps",
60 examples: &[
61 "dec map[string, array[int]] rosters = {\"team_a\": [1, 2, 3]}\ndec map[string, map[string, int]] nested = {\"round1\": {\"alice\": 10}}",
62 ],
63 expected_output: &[],
64 },
65 DescriptionEntry {
66 kind: DescriptionKind::Pitfall,
67 title: None,
68 description: "reading a key that doesn't exist is a runtime error, not `null` - there is no `map_get`-with-default yet, so check with a lookup helper once the map stdlib module exists, or guard with a `match`/`if`",
69 examples: &[],
70 expected_output: &[],
71 },
72 DescriptionEntry {
73 kind: DescriptionKind::Note,
74 title: None,
75 description: "there's no iteration syntax for maps yet (no `for key, value in map`), and no stdlib module (`map_get`, `map_keys`, `map_values`, `map_has`, `map_remove`) - only literal construction and single-level `map[key]` read/write are currently supported",
76 examples: &[],
77 expected_output: &[],
78 },
79 ],
80 pitfalls: &[
81 "assigning through more than one level of nesting, e.g. `arr[0][\"key\"] = value` where the outer container is an array of maps, is not yet supported - only a bare `map[key] = value` works",
82 ],
83 related: &["arrays", "types"],
84 related_stdlib: &[],
85 since: Some("v0.1.5"),
86};