rl_lang/docs/entries/concepts/errors/
mod.rs1use crate::docs::entry::{ConceptCategory, ConceptEntry, DescriptionEntry, DescriptionKind};
2
3pub static ERROR_TYPE: ConceptEntry = ConceptEntry {
4 name: "result",
5 descriptions: &[
6 DescriptionEntry {
7 description: "`result[T]` is a type that holds either `ok(value)` on success or `err(value)` on failure",
8 examples: &[
9 "dec result[int] r = ok(42)",
10 "dec result[string] e = err(\"not found\")",
11 "dec result[int] r = err(404)",
12 ],
13 kind: DescriptionKind::Explanation,
14 title: None,
15 expected_output: &[],
16 },
17 DescriptionEntry {
18 description: "`ok(value)` and `err(value)` are the two constructors; both wrap any non-result value",
19 examples: &[
20 "dec result[int] r = ok(100)",
21 "dec result[int] r = err(0)",
22 "dec result[string] r = ok(\"hello\")",
23 ],
24 kind: DescriptionKind::Explanation,
25 title: None,
26 expected_output: &[],
27 },
28 DescriptionEntry {
29 description: "check which variant you have with `is_ok` and `is_err` from `std::res`",
30 examples: &[
31 "get is_ok, is_err from std::res\ndec result[int] r = ok(42)\nprintln(is_ok(r)) // true\nprintln(is_err(r)) // false",
32 ],
33 kind: DescriptionKind::Explanation,
34 title: None,
35 expected_output: &[],
36 },
37 DescriptionEntry {
38 description: "extract the inner value with `result_unwrap` (ok) or `result_unwrap_err` (err) from `std::res`",
39 examples: &[
40 "get result_unwrap from std::res\ndec result[int] r = ok(10)\nprintln(result_unwrap(r)) // 10",
41 "get result_unwrap_err from std::res\ndec result[int] r = err(\"oops\")\nprintln(result_unwrap_err(r)) // oops",
42 ],
43 kind: DescriptionKind::Explanation,
44 title: None,
45 expected_output: &[],
46 },
47 DescriptionEntry {
48 description: "`result_unwrap_or(r, default)` returns the ok value or a fallback if err",
49 examples: &[
50 "get result_unwrap_or from std::res\ndec result[int] r = err(\"fail\")\nprintln(result_unwrap_or(r, 0)) // 0",
51 ],
52 kind: DescriptionKind::Explanation,
53 title: None,
54 expected_output: &[],
55 },
56 DescriptionEntry {
57 description: "functions that may fail should return `result[T]` and the caller checks with `is_ok` / `is_err`",
58 examples: &[
59 "fn divide(int a, int b) -> result[int] {\n if b == 0 {\n return err(\"division by zero\")\n }\n return ok(a / b)\n}\n\ndec result[int] r = divide(10, 2)\nif is_ok(r) {\n println(result_unwrap(r)) // 5\n}",
60 ],
61 kind: DescriptionKind::Explanation,
62 title: None,
63 expected_output: &[],
64 },
65 DescriptionEntry {
66 description: "`result_map` transforms the ok value; `result_map_err` transforms the err value - both pass the other variant through unchanged",
67 examples: &[
68 "get result_map from std::res\ndec result[int] r = ok(5)\ndec result[int] r2 = result_map(r, fn(int x) -> int { return x * 2 })\nprintln(result_unwrap(r2)) // 10",
69 ],
70 kind: DescriptionKind::Explanation,
71 title: None,
72 expected_output: &[],
73 },
74 DescriptionEntry {
75 description: "`error[T]` is a separate wrapper type from `result[T]` - construct it with `error(value)`, check with `is_error` from `std::res`, and unwrap with `error_unwrap`",
76 examples: &[
77 "get is_error, error_unwrap from std::res\ndec error e = error(\"disk full\")\nprintln(is_error(e)) // true\nprintln(error_unwrap(e)) // \"disk full\"",
78 ],
79 kind: DescriptionKind::Explanation,
80 title: None,
81 expected_output: &[],
82 },
83 DescriptionEntry {
84 description: "an `error` cannot wrap another `error` - this raises a runtime error",
85 examples: &["dec error e = error(error(\"oops\")) // runtime error"],
86 kind: DescriptionKind::Pitfall,
87 title: None,
88 expected_output: &[],
89 },
90 ],
91 summary: "",
92 category: ConceptCategory::Syntax,
93 prerequisites: &[],
94 pitfalls: &[],
95 related: &[],
96 related_stdlib: &[],
97 since: None,
98};