From 2ea3265a743a89137ddf559a23789bdd06945e9b Mon Sep 17 00:00:00 2001 From: Neil Henderson <2060747+bluetarpmedia@users.noreply.github.com> Date: Tue, 20 Feb 2024 11:34:14 +1000 Subject: [PATCH 1/2] Update expressions.md Signed-off-by: Neil Henderson <2060747+bluetarpmedia@users.noreply.github.com> --- docs/cpp2/expressions.md | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/docs/cpp2/expressions.md b/docs/cpp2/expressions.md index b66874a78e..6fb90c5909 100644 --- a/docs/cpp2/expressions.md +++ b/docs/cpp2/expressions.md @@ -212,11 +212,13 @@ For more examples, see also the examples in the previous two sections on `is` an ## `$` — captures, including interpolations -Suffix `$` is pronounced **"paste the value of"** and captures the value of an expression at the point when the expression where the capture is written is evaluated. Depending the complexity of the capture expression `expr$` and where it is used, parentheses `(expr)$` may be required for precedence or to show the boundaries of the expression. +Suffix `$` is pronounced **"paste the value of"** and captures the value of an expression at the point when the expression where the capture is written is evaluated. Depending on the complexity of the capture expression `expr$` and where it is used, parentheses `(expr)$` may be required for precedence or to show the boundaries of the expression. `x$` always captures `x` by value. To capture by reference, take the address and capture a pointer using `x&$`. If the value is immediately used, dereference again; for example `:(val) total&$* += val` adds to the `total` local variable itself, not a copy. -Any capture is evaluated at the point where it is written, in the function expression, contract postcondition, or string literal. The stored captured value can then be used later when the context it is in is evaluated, such as when the function expression body it's in is actually called later (one or more times), when the postcondition it's in is evaluated later when the function returns, or when the string literal it's in is read later. +Captures are evaluated at the point where they are written in function expressions, contract postconditions, and string literals. The stored captured value can then be used later when evaluating its context, such as when the function expression body containing the captured value is actually called later (one or more times), when the postcondition containing the captured value is evaluated later when the function returns, or when the string literal containing the captured value is read later. + +The design and syntax are selected so that capture is spelled the same way in all contexts. For details, see [Design note: Capture](https://github.com/hsutter/cppfront/wiki/Design-note%3A-Capture). ### Capture in function expressions (aka lambdas) @@ -235,9 +237,33 @@ main: () = { // Function capture: Paste local variable value ); } + +// prints: +// 1-sh +// 2-sh +// 3-sh +// 5-sh +// 8-sh +// 13-sh ``` -The design and syntax are selected so that capture is spelled the same way in all contexts. For details, see [Design note: Capture](https://github.com/hsutter/cppfront/wiki/Design-note%3A-Capture). +Another example: + +``` cpp title="Capture in a named function expression (aka lambda)" hl_lines="2 4 7" +main: () = { + price := 100; + func := :() = { + std::cout << "Price = " << price$ << "\n"; + }; + func(); + price = 200; + func(); +} + +// prints: +// Price = 100 +// Price = 100 +``` ### Capture in contract postconditions From 763f2e747d430147e2efbb624517387c4465bb38 Mon Sep 17 00:00:00 2001 From: Herb Sutter Date: Sat, 24 Feb 2024 16:30:29 -0800 Subject: [PATCH 2/2] Update highlighted linenos And a couple of other fixes, including that I meant to write "ish" not "sh" (fixing my own typo!) --- docs/cpp2/expressions.md | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/docs/cpp2/expressions.md b/docs/cpp2/expressions.md index 6fb90c5909..4343858c14 100644 --- a/docs/cpp2/expressions.md +++ b/docs/cpp2/expressions.md @@ -227,29 +227,30 @@ Any capture in a function expression body is evaluated at the point where the fu For example: -``` cpp title="Capture in an unnamed function expression (aka lambda)" hl_lines="6 7" +``` cpp title="Capture in an unnamed function expression (aka lambda)" hl_lines="7 8 13-18" main: () = { - s := "-sh\n"; + s := "-ish\n"; vec: std::vector = (1, 2, 3, 5, 8, 13 ); - vec.std::ranges::for_each( - :(i) = { std::cout << i << s$; } + std::ranges::for_each( + vec, + :(i) = std::cout << i << s$ // Function capture: Paste local variable value ); } // prints: -// 1-sh -// 2-sh -// 3-sh -// 5-sh -// 8-sh -// 13-sh +// 1-ish +// 2-ish +// 3-ish +// 5-ish +// 8-ish +// 13-ish ``` Another example: -``` cpp title="Capture in a named function expression (aka lambda)" hl_lines="2 4 7" +``` cpp title="Capture in a named function expression (aka lambda)" hl_lines="2 4 7 12 13" main: () = { price := 100; func := :() = {