Skip to content

Commit 3a8874a

Browse files
committed
Docs: Fix UFCS interpolation example
Note that "interpolate('d')$ string".ufcs_call() now requires parens, ("interpolate('d')$ string").ufcs_call()
1 parent 6f666ba commit 3a8874a

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

docs/cpp2/expressions.md

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,27 @@ f: ( v : std::vector<widget> ) = {
2727
log( v.ssize() );
2828
}
2929

30-
// Generic function to print "hello, ___!" for any printable type
31-
hello: (name) = {
32-
myfile := fopen("xyzzy.txt", "w");
33-
// Direct calls to C nonmember functions, using UFCS and safe
34-
// string interpolation (instead of type-unsafe format strings)
35-
myfile.fprintf( "Hello, (name)$!\n" );
36-
myfile.fclose();
30+
// Generic function to use standard I/O to print any printable types
31+
// safely using string interpolation (instead of type-unsafe format strings)
32+
hello: (name, height: float) = {
33+
// Using UFCS to make direct calls to C functions as if they were members
34+
stdout.fprintf( "%s", ("Hello (name)$, your height is (height:.1f)$ inches!\n").c_str() );
35+
// Equivalent using iostreams:
36+
// std::cout << "Hello (name)$, your height is (height:.1f)$ inches!\n";
37+
3738
// The C and C++ standard libraries are not only fully available,
3839
// but safer (and arguably nicer) when used from Cpp2 syntax code
3940
}
41+
42+
main: () = {
43+
hello("Flimnap", 6.5);
44+
hello("Goliath", 115);
45+
hello("Polyphemus", 180);
46+
}
47+
// Sample output:
48+
// Hello Flimnap, your height is 6.5 inches!
49+
// Hello Goliath, your height is 115.0 inches!
50+
// Hello Polyphemus, your height is 180.0 inches!
4051
```
4152

4253
To explicitly treat an object name passed as an argument as `move` or `out`, write that keyword before the variable name.

0 commit comments

Comments
 (0)