Skip to content

Commit d8d8a5a

Browse files
committed
Function outputs and explicit discard
1 parent 1b7017a commit d8d8a5a

File tree

3 files changed

+60
-5
lines changed

3 files changed

+60
-5
lines changed

docs/cpp2/common.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ As always, `main` is the entry point of the program. For example:
88

99
- No parameters: `main: () /*etc.*/`
1010

11-
- One parameter of implicit type named `args`: `main: (args) /*etc*/`.
11+
- One parameter of implicit type named `args`: `main: (args) /*etc.*/`
1212
- The type of `args` cannot be explicitly specified. It is always `cpp2::args_t`, which behaves similarly to a `const std::array<std::string_view>`.
1313
- Using `args` performs zero heap allocations. Every `string_view` is directly bound to the string storage provided by host environment.
14-
- `args.argc` and `args.argv` dditionally provide access to the raw C/C++ `main` parameters.
14+
- `args.argc` and `args.argv` additionally provide access to the raw C/C++ `main` parameters.
1515

1616
``` cpp title="Examples: main with (args)"
1717
// Print out command line arguments, then invoke

docs/cpp2/functions.md

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,66 @@ f: ( v : std::vector<widget> ) = {
2525
}
2626
```
2727

28+
If the function has an output that cannot be silently discarded, you can
2829

29-
## Parameter passing
30+
31+
## Parameters
32+
33+
TODO
34+
35+
> Note: All parameters and other objects in Cpp2 are `const` by default, except for local variables. For details, see [Design note: `const` objects by default](https://github.com/hsutter/cppfront/wiki/Design-note%3A-const-objects-by-default).
36+
37+
38+
## Return values
3039

3140
TODO
3241

33-
All parameters and other objects in Cpp2 are `const` by default, except for local variables. For details, see [Design note: `const` objects by default](https://github.com/hsutter/cppfront/wiki/Design-note%3A-const-objects-by-default).
42+
43+
### Function outputs
44+
45+
A function's outputs are its return values, and the "out" state of any `out` and `inout` parameters.
46+
47+
Function outputs cannot be silently discarded. To explicitly discard a function output, assign it to `_`. For example:
48+
49+
``` cpp title="Example: No silent discard"
50+
f: () -> void = { }
51+
g: () -> int = { return 10; }
52+
h: (inout x: int) -> void = { x = 20; }
53+
54+
main: ()
55+
= {
56+
f(); // ok, no return value
57+
58+
std::cout << g(); // ok, use return value
59+
_ = g(); // ok, explicitly discard return value
60+
g(); // ERROR, return value is ignored
61+
62+
{
63+
x := 0;
64+
h( x ); // ok, x is referred to again...
65+
std::cout << x; // ... here, so its new value is used
66+
}
67+
68+
{
69+
x := 0;
70+
h( x ); // ok, x is referred to again...
71+
_ = x; // ... here, its value explicitly discarded
72+
}
73+
74+
{
75+
x := 0;
76+
h( x ); // ERROR, this is a definite last use of x
77+
} // so x is not referred to again, and its
78+
// 'out' value can't be implicitly discarded
79+
}
80+
```
81+
82+
> Cpp2 imbues Cpp1 code with nondiscardable semantics, while staying fully compatible as usual:
83+
>
84+
> - A function written in Cpp2 syntax that returns something other than `void` is always compiled to Cpp1 with `[[nodiscard]]`.
85+
>
86+
> - A function call written in Cpp2 `x.f()` member call syntax always treats a non-`void` return type as not discardable, even if the function was written in Cpp1 syntax that did not write `[[nodiscard]]`.
87+
3488

3589
## Control flow
3690

docs/cpp2/objects.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,9 @@ f: () -> std::shared_ptr<widget>
4545
// Dynamically allocate an object owned by a std::unique_ptr
4646
// 'vec' is a unique_ptr<vector<i32>> containing three values
4747
vec := new<std::vector<i32>>(1, 2, 3);
48-
// shorthand for 'unique.new<...>(...)'
48+
// shorthand for 'unique.new<...>(...)'
4949
std::cout << vec*.ssize(); // prints 3
50+
// note that * dereference is a suffix operator
5051

5152
// Dynamically allocate an object with shared ownership
5253
wid := cpp2::shared.new<widget>();

0 commit comments

Comments
 (0)