rl_lang/docs/entries/concepts/sets/
mod.rs1use crate::docs::entry::{ConceptCategory, ConceptEntry, DescriptionEntry, DescriptionKind};
2
3pub static SETS: ConceptEntry = ConceptEntry {
4 name: "sets",
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 set with `dec set[<item type>] <n> = {<item>, ...}` - the type in brackets is the shared type of every item",
13 examples: &[
14 "dec set[int] scores = {95, 82, 71}",
15 "dec set[string] names = {\"alice\", \"bob\"}",
16 "dec set[bool] flags = {true, false}",
17 ],
18 expected_output: &[],
19 },
20 DescriptionEntry {
21 kind: DescriptionKind::Explanation,
22 title: None,
23 description: "declare a constant set the same way with `const set[<item type>] <NAME> = {...}`",
24 examples: &["const set[int] LUCKY_NUMBERS = {3, 7, 21}"],
25 expected_output: &[],
26 },
27 DescriptionEntry {
28 kind: DescriptionKind::Explanation,
29 title: None,
30 description: "read an item by its position with `set[index]`, exactly like an array - the first item is index 0",
31 examples: &["dec set[int] scores = {95, 82, 71}\nprintln(scores[0]) // 95"],
32 expected_output: &[],
33 },
34 DescriptionEntry {
35 kind: DescriptionKind::Explanation,
36 title: None,
37 description: "sets are typed like arrays: every item must share one type, checked against the declared item type",
38 examples: &[
39 "dec set[int] scores = {95, 82}\n// dec set[int] bad = {95, \"not a number\"} // error: type mismatch",
40 ],
41 expected_output: &[],
42 },
43 DescriptionEntry {
44 kind: DescriptionKind::Pitfall,
45 title: None,
46 description: "unlike a mathematical set, duplicate items are not removed and are not rejected - `{1, 1, 1}` is a valid 3-item set with no uniqueness check performed at declaration or runtime",
47 examples: &["dec set[int] xs = {1, 1, 1} // length is 3, not 1"],
48 expected_output: &[],
49 },
50 DescriptionEntry {
51 kind: DescriptionKind::Pitfall,
52 title: None,
53 description: "`set[index] = value` is rejected - sets do not support index assignment at all, unlike arrays and maps",
54 examples: &[
55 "dec set[int] scores = {95, 82}\n// scores[0] = 100 // error: sets does not support index assigning",
56 ],
57 expected_output: &[],
58 },
59 DescriptionEntry {
60 kind: DescriptionKind::Pitfall,
61 title: None,
62 description: "a set literal `{...}` is only recognized in a `dec set[T]`/`const set[T]` declaration's initializer - writing `{1, 2, 3}` anywhere else (a plain assignment, a function argument, nested inside an array or map literal) is parsed as a map literal instead and fails with `expected ':' after map key`",
63 examples: &[
64 "dec set[int] xs = {1, 2, 3} // ok - dedicated declaration syntax\n// xs = {4, 5, 6} // error: parsed as a map literal, not a set",
65 ],
66 expected_output: &[],
67 },
68 DescriptionEntry {
69 kind: DescriptionKind::Note,
70 title: None,
71 description: "unlike map keys, set items are not restricted to hashable types - any type usable in an array (including nested arrays, records, or other sets) can be used as a set item, since a set is stored the same way as an array under the hood",
72 examples: &[],
73 expected_output: &[],
74 },
75 DescriptionEntry {
76 kind: DescriptionKind::Note,
77 title: None,
78 description: "there's no iteration syntax for sets yet (no `for item in set`), and no stdlib module (`set_add`, `set_contains`, `set_remove`, `set_union`, `set_intersect`) - only declaration-site construction and positional `set[index]` reads are currently supported",
79 examples: &[],
80 expected_output: &[],
81 },
82 ],
83 pitfalls: &[
84 "sets don't enforce uniqueness - think of `set[T]` today as sugar for a fixed, non-index-assignable array, not a mathematical set",
85 "a set literal only parses correctly as the initializer of a `dec set[T]`/`const set[T]` declaration - it is not yet a general-purpose expression",
86 ],
87 related: &["arrays", "maps", "types"],
88 related_stdlib: &[],
89 since: Some("v0.1.5"),
90};