@@ -27,16 +27,27 @@ f: ( v : std::vector<widget> ) = {
27
27
log ( v.ssize() );
28
28
}
29
29
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
+
37
38
// The C and C++ standard libraries are not only fully available,
38
39
// but safer (and arguably nicer) when used from Cpp2 syntax code
39
40
}
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!
40
51
```
41
52
42
53
To explicitly treat an object name passed as an argument as ` move ` or ` out ` , write that keyword before the variable name.
0 commit comments