rl_lang/docs/entries/concepts/lambdas/
mod.rs1use crate::docs::entry::{ConceptCategory, ConceptEntry, DescriptionEntry, DescriptionKind};
2
3pub static LAMBDAS: ConceptEntry = ConceptEntry {
4 name: "lambdas",
5 summary: "anonymous, inline functions that can capture their surrounding scope",
6 category: ConceptCategory::Functions,
7 prerequisites: &["functions", "variables", "types"],
8 descriptions: &[
9 DescriptionEntry {
10 kind: DescriptionKind::Explanation,
11 title: Some("defining a lambda"),
12 description: "lambdas are anonymous functions defined inline with `fn(<type> <param>, ...) { <body> }`",
13 examples: &[
14 "dec fn square = fn(int x) -> int {\n return x * x\n}\n\nprintln(square(5)) // 25",
15 ],
16 expected_output: &["25"],
17 },
18 DescriptionEntry {
19 kind: DescriptionKind::Explanation,
20 title: Some("closures"),
21 description: "lambdas capture variables from their surrounding scope (closures)",
22 examples: &[
23 "dec int factor = 3\n\ndec fn triple = fn(int x) -> int {\n return x * factor\n}\n\nprintln(triple(4)) // 12",
24 ],
25 expected_output: &["12"],
26 },
27 DescriptionEntry {
28 kind: DescriptionKind::Pitfall,
29 title: Some("captured values are snapshotted, not live"),
30 description: "a lambda captures the value of a variable at creation time - reassigning the outer variable afterward does not change what the lambda sees",
31 examples: &[
32 "dec int factor = 3\ndec fn triple = fn(int x) -> int {\n return x * factor\n}\n\nfactor = 100\nprintln(triple(4)) // still 12, not 400",
33 ],
34 expected_output: &["12"],
35 },
36 ],
37 pitfalls: &["captured variables are snapshotted at creation, not live-referenced"],
38 related: &["functions", "closures"],
39 related_stdlib: &[],
40 since: None,
41};