Skip to main content

rl_lang/docs/entries/concepts/propagate/
mod.rs

1use crate::docs::entry::{ConceptCategory, ConceptEntry, DescriptionEntry, DescriptionKind};
2
3pub static PROPAGATE: ConceptEntry = ConceptEntry {
4    name: "propagation",
5    descriptions: &[
6        DescriptionEntry {
7            description: "`expr?` unwraps a `result[T]`: on `ok(v)` it evaluates to `v`, on `err(e)` it immediately returns `err(e)` from the enclosing function",
8            examples: &[
9                "fn parse_positive(int n) -> result[int] {\n    if n < 0 {\n        return err(\"negative\")\n    }\n    return ok(n)\n}\n\nfn double_positive(int n) -> result[int] {\n    dec int v = parse_positive(n)?  // returns err early if parse_positive fails\n    return ok(v * 2)\n}",
10            ],
11            kind: DescriptionKind::Explanation,
12            title: None,
13            expected_output: &[],
14        },
15        DescriptionEntry {
16            description: "`?` avoids manual `is_ok`/`is_err` checks for chains of fallible calls",
17            examples: &[
18                "get is_ok, result_unwrap from std::res\n\nfn without_propagate(int n) -> result[int] {\n    dec result[int] r = parse_positive(n)\n    if is_ok(r) {\n        return ok(result_unwrap(r) * 2)\n    }\n    return r\n}\n\nfn with_propagate(int n) -> result[int] {\n    return ok(parse_positive(n)? * 2)\n}",
19            ],
20            kind: DescriptionKind::Explanation,
21            title: None,
22            expected_output: &[],
23        },
24        DescriptionEntry {
25            description: "`?` on a non-`result` value is a no-op - it only short-circuits on `err`",
26            examples: &["dec int x = 5?  // no-op, x = 5"],
27            kind: DescriptionKind::Pitfall,
28            title: None,
29            expected_output: &[],
30        },
31        DescriptionEntry {
32            description: "the enclosing function should be declared to return `result[T]`, since `?` may return an `err` value directly out of it",
33            examples: &[],
34            kind: DescriptionKind::Pitfall,
35            title: None,
36            expected_output: &[],
37        },
38    ],
39    summary: "",
40    category: ConceptCategory::ErrorHandling,
41    prerequisites: &["result"],
42    pitfalls: &[],
43    related: &["result"],
44    related_stdlib: &["result"],
45    since: None,
46};