Skip to content

Commit 9f75d36

Browse files
committed
Move function calls to expressions
1 parent 4495b65 commit 9f75d36

File tree

2 files changed

+46
-44
lines changed

2 files changed

+46
-44
lines changed

docs/cpp2/expressions.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,52 @@
11

22
# Common expressions
33

4+
## Calling functions: `f(x)` syntax, and `x.f()` UFCS syntax
5+
6+
A function call like `f(x)` is a normal function call that will call non-member functions only, as usual in C++.
7+
8+
A function call like `x.f()` is a unified function call syntax (aka UFCS) call. It will call a member function if one is available, and otherwise will call `f(x)`. Having UFCS is important for generic code that may want to call a member or a non-member function, whichever is available. It's also important to enable fluid programming styles and natural IDE autocompletion support.
9+
10+
An operator notation call like `#!cpp a + b` will call an overloaded operator function if one is available, as usual in C++.
11+
12+
For example:
13+
14+
``` cpp title="Function calls" hl_lines="3 7 11 16 19 20"
15+
// Generic function to log something
16+
// This calls operator<< using operator notation
17+
log: (x) = clog << x;
18+
19+
f: ( v : std::vector<widget> ) = {
20+
// This calls log() with the result of std::vector::size()
21+
log( v.size() );
22+
23+
// This calls log() with the result of std::ssize(v), because
24+
// v doesn't have a .ssize member function
25+
log( v.ssize() );
26+
}
27+
28+
// Generic function to print "hello, ___!" for any printable type
29+
hello: (name) = {
30+
myfile := fopen("xyzzy.txt", "w");
31+
// Direct calls to C nonmember functions, using UFCS and safe
32+
// string interpolation (instead of type-unsafe format strings)
33+
myfile.fprintf( "Hello, (name)$!\n" );
34+
myfile.fclose();
35+
// The C and C++ standard libraries are not only fully available,
36+
// but safer (and arguably nicer) when used from Cpp2 syntax code
37+
}
38+
```
39+
40+
To explicitly treat an object name passed as an argument as `move` or `out`, write that keyword before the variable name.
41+
42+
- Explicit `move` is rarely needed. Every definite last use of a local variable will apply `move` by default. Writing `move` from an object before its definite last use means that later uses may see a moved-from state.
43+
44+
- Explicit `out` is needed only when initializing a local variable separately from its declaration using a call to a function with an `out` parameter. For details, see [Guaranteed initialization](../cpp2/objects.md#Init).
45+
46+
For example:
47+
48+
49+
450
## `_` — the "don't care" wildcard, including explicit discard
551

652
`_` is pronounced **"don't care"** and allowed as a wildcard in most contexts. For example:

docs/cpp2/functions.md

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -5,50 +5,6 @@
55

66
TODO
77

8-
## Calling functions: `f(x)` syntax, and `x.f()` UFCS syntax
9-
10-
A function call like `f(x)` is a normal function call that will call non-member functions only, as usual in C++.
11-
12-
A function call like `x.f()` is a unified function call syntax (aka UFCS) call. It will call a member function if one is available, and otherwise will call `f(x)`. Having UFCS is important for generic code that may want to call a member or a non-member function, whichever is available. It's also important to enable fluid programming styles and natural IDE autocompletion support.
13-
14-
An operator notation call like `#!cpp a + b` will call an overloaded operator function if one is available, as usual in C++.
15-
16-
For example:
17-
18-
``` cpp title="Function calls" hl_lines="3 7 11 16 19 20"
19-
// Generic function to log something
20-
// This calls operator<< using operator notation
21-
log: (x) = clog << x;
22-
23-
f: ( v : std::vector<widget> ) = {
24-
// This calls log() with the result of std::vector::size()
25-
log( v.size() );
26-
27-
// This calls log() with the result of std::ssize(v), because
28-
// v doesn't have a .ssize member function
29-
log( v.ssize() );
30-
}
31-
32-
// Generic function to print "hello, ___!" for any printable type
33-
hello: (name) = {
34-
myfile := fopen("xyzzy.txt", "w");
35-
// Direct calls to C nonmember functions, using UFCS and safe
36-
// string interpolation (instead of type-unsafe format strings)
37-
myfile.fprintf( "Hello, (name)$!\n" );
38-
myfile.fclose();
39-
// The C and C++ standard libraries are not only fully available,
40-
// but safer (and arguably nicer) when used from Cpp2 syntax code
41-
}
42-
```
43-
44-
To explicitly treat an object name passed as an argument as `move` or `out`, write that keyword before the variable name.
45-
46-
- Explicit `move` is rarely needed. Every definite last use of a local variable will apply `move` by default. Writing `move` from an object before its definite last use means that later uses may see a moved-from state.
47-
48-
- Explicit `out` is needed only when initializing a local variable separately from its declaration using a call to a function with an `out` parameter. For details, see [Guaranteed initialization](../cpp2/objects.md#Init).
49-
50-
For example:
51-
528

539
## Parameters
5410

0 commit comments

Comments
 (0)