Skip to content

Commit 79252d4

Browse files
committed
Add interpolation formatting, and other minor cleanup
1 parent 430afe0 commit 79252d4

File tree

2 files changed

+15
-24
lines changed

2 files changed

+15
-24
lines changed

docs/cpp2/expressions.md

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ For more examples, see also the examples in the previous two sections on `is` an
212212
213213
## `$` — captures, including interpolations
214214
215-
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.
215+
Suffix `$` is pronounced **"paste the value"** 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.
216216
217217
`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.
218218
@@ -225,14 +225,14 @@ Any capture in a function expression body is evaluated at the point where the fu
225225
226226
For example:
227227
228-
``` cpp title="Capture in an unnamed function expression (aka lambda)" hl_lines="6 7"
228+
``` cpp title="Capture in an unnamed function expression (aka lambda)" hl_lines="6"
229229
main: () = {
230230
s := "-sh\n";
231231
vec: std::vector = (1, 2, 3, 5, 8, 13 );
232232
233233
vec.std::ranges::for_each(
234234
:(i) = { std::cout << i << s$; }
235-
// Function capture: Paste local variable value
235+
// Paste the value of `s`
236236
);
237237
}
238238
```
@@ -246,10 +246,10 @@ Any capture in a postcondition is evaluated at the point where the postcondition
246246

247247
For example:
248248

249-
``` cpp title="Capture in contract postconditions" hl_lines="2 3"
249+
``` cpp title="Capture in contract postconditions" hl_lines="2"
250250
push_back: (coll, value)
251251
[[post: coll.ssize() == coll.ssize()$ + 1]]
252-
// Postcondition capture: Paste "old" size
252+
// Paste the value of `coll.ssize()`
253253
= {
254254
// ...
255255
}
@@ -258,22 +258,23 @@ push_back: (coll, value)
258258

259259
### Capture in string interpolation
260260

261-
A string literal can capture the value of an expression `expr` by writing `(expr)$` inside the string literal. The `(` `)` are required, and cannot be nested.
261+
A string literal can capture the value of an expression `expr` by writing `(expr)$` inside the string literal. The `(` `)` are required, and cannot be nested. A string literal has type `std::string` if it performs any captures, otherwise it is a normal C/C++ string literal (array of characters).
262262

263263
Any capture in a string literal is evaluated at the point where the string literal is written. The string literal can be used repeatedly later, and includes the captured value.
264264

265265
For example:
266266

267-
``` cpp title="Capture for string interpolation" hl_lines="2 4"
267+
``` cpp title="Capture for string interpolation" hl_lines="2 5"
268268
x := 0;
269269
std::cout << "x is (x)$\n";
270+
// Paste the value of `x`
270271
x = 1;
271272
std::cout << "now x+2 is (x+2)$\n";
273+
// Paste the value of `x+2`
272274

273275
// prints:
274276
// x is 0
275277
// now x+2 is 3
276278
```
277279

278-
A string literal has type `std::string` if it performs any captures, otherwise it is a normal C/C++ string literal (array of characters).
279-
280+
A string literal capture can include a `:suffix` where the suffix is a [standard C++ format specification](https://en.cppreference.com/w/cpp/utility/format/spec). For example, `#!cpp (x.price(): <10.2f)$` evaluates `x.price()` and converts the result to a string with 10-character width, 2 digits of precision, and left-justified.

docs/welcome/hello-world.md

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,11 @@
11
# **Hello, world!**
22

3-
<table><tr><td>
4-
``` cpp title="hello.cpp2"
5-
main: () = {
6-
std::cout << "Hello, world!\n";
7-
}
8-
```
9-
</td><td>
103
``` mermaid
11-
graph TB
12-
A[hello.cpp2] ==> B([cppfront]);
4+
graph LR
5+
A[hello.cpp2] ==> B(["` **cppfront** `"]);
136
B ==> C[hello.cpp];
14-
C ==> D([Your favorite C++ compiler]);
7+
C ==> D([Your favorite<br> C++ compiler<p>... and IDE / libraries / build<br>system / in-house tools / ...]);
158
```
16-
</td></tr></table>
179

1810
## A `hello.cpp2` program
1911

@@ -30,7 +22,6 @@ main: () = {
3022
words: std::vector = ( "Alice", "Bob" );
3123
hello( words[0] );
3224
hello( words[1] );
33-
std::cout << "... and goodnight\n";
3425
}
3526

3627
hello: (msg: std::string_view) =
@@ -98,7 +89,6 @@ auto main() -> int{
9889
std::vector words {"Alice", "Bob"};
9990
hello(CPP2_ASSERT_IN_BOUNDS_LITERAL(words, 0));
10091
hello(CPP2_ASSERT_IN_BOUNDS_LITERAL(std::move(words), 1));
101-
std::cout << "... and goodnight\n";
10292
}
10393

10494
auto hello(cpp2::in<std::string_view> msg) -> void {
@@ -116,7 +106,7 @@ Here we can see more of how Cpp2 makes it features work.
116106
- **Line 9: CTAD** just works, because it turns into ordinary C++ code which already supports CTAD.
117107
- **Lines 10-11: Automatic bounds checking** is added to `#!cpp words[0]` and `#!cpp words[1]` nonintrusively at the call site by default. Because it's nonintrusive, it works seamlessly with all existing container types that are `std::size` and `std::ssize`-aware, when you use them from safe Cpp2 code.
118108
- **Line 11: Automatic move from last use** ensures the last use of `words` will automatically avoid a copy if it's being passed to something that's optimized for rvalues.
119-
- **Line 16: String interpolation** performs the string capture of `msg`'s current value via `cpp2::to_string`. That uses `std::to_string` when available, and it also works for additional types (such as `#!cpp bool`, to print `#!cpp false` and `#!cpp true` instead of `0` and `1`, without having to remember to use `std::boolalpha`).
109+
- **Line 15: String interpolation** performs the string capture of `msg`'s current value via `cpp2::to_string`. That uses `std::to_string` when available, and it also works for additional types (such as `#!cpp bool`, to print `#!cpp false` and `#!cpp true` instead of `0` and `1`, without having to remember to use `std::boolalpha`).
120110
121111
**How: Simplicity through generality + defaults.**
122112
@@ -128,7 +118,7 @@ Here we can see more of how Cpp2 makes it features work.
128118
129119
**How: Seamless compatibility and interop.**
130120
131-
- **Lines 9, 12, and 16: Ordinary direct calls** to existing C++ code, so there's never a need for wrapping/marshaling/thunking.
121+
- **Lines 9-11 and 16: Ordinary direct calls** to existing C++ code, so there's never a need for wrapping/marshaling/thunking.
132122
133123
**How: C++ standard library always available.**
134124

0 commit comments

Comments
 (0)