You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
For more details, see [Design note: Postfix unary operators vs binary operators](https://github.com/hsutter/cppfront/wiki/Design-note%3A-Postfix-unary-operators-vs-binary-operators).
An `inspect expr -> Type` expression allows pattern matching using `is`.
176
+
An `inspect expr -> Type = { /* alternatives */ }` expression allows pattern matching using `is`.
177
177
178
178
-`expr` is evaluated once.
179
179
180
-
- Each alternative spelled `is C` is evaluated in order as if called with `expr is C`.
181
-
182
-
- If an alternative evaluates to `#!cpp true`, then its `#!cpp = alternative;` body is used as the value of the entire `inspect` expression, and the meaning is the same as if the entire `inspect` expression had been written as just `#!cpp :Type = alternative;` — i.e., an unnamed object expression (aka 'temporary object') of type `Type` initialized with `alternative`.
180
+
- Each alternative is spelled `is C = statement;` and are evaluated in order. Each `is C` is evaluated as if called with `expr is C`, and if it evaluates to `#!cpp true`, then its `#!cpp = alternative;` body is used as the value of the entire `inspect` expression, and the meaning is the same as if the entire `inspect` expression had been written as just `#!cpp :Type = alternative;` — i.e., an unnamed object expression (aka 'temporary object') of type `Type` initialized with `alternative`.
183
181
184
182
- A catchall `is _` is required.
185
183
@@ -277,7 +275,7 @@ For example:
277
275
278
276
```cpp title="Capture in contract postconditions" hl_lines="2"
Copy file name to clipboardExpand all lines: docs/cpp2/functions.md
+40-5Lines changed: 40 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -27,20 +27,39 @@ func: (
27
27
}
28
28
```
29
29
30
-
There are six ways to pass parameters that cover all use cases:
30
+
There are six ways to pass parameters that cover all use cases, that can be written before the parameter name:
31
31
32
32
| Parameter ***kind***| "Pass me an `x` I can ______" | Accepts arguments that are | Special semantics |***kind***`x: X` Compiles to Cpp1 as |
33
33
|---|---|---|---|---|
34
34
|`in` (default) | read from | anything | always `#!cpp const`<p>automatically passes by value if cheaply copyable |`X const x` or `X const& x`|
35
35
|`copy`| take a copy of | anything | acts like a normal local variable initialized with the argument |`X x`|
36
36
|`inout`| read from and write to | lvalues ||`X& x`|
37
-
|`out`| write to (including construct) | lvalues, including uninitialized lvalues | must `=` assign/construct before other uses |`cpp2::out<X>`|
38
-
|`move`| move from | rvalues | automatically moves from every definite last use |`X&&`|
37
+
|`out`| write to (including construct) | lvalues (including uninitialized) | must `=` assign/construct before other uses |`cpp2::impl::out<X>`|
38
+
|`move`| move from (consume the value of) | rvalues | automatically moves from every definite last use |`X&&`|
39
39
|`forward`| forward | anything | automatically forwards from every definite last use |`T&&` constrained to type `X`|
40
40
41
-
42
41
> Note: All parameters and other objects in Cpp2 are `#!cpp const` by default, except for local variables. For details, see [Design note: `#!cpp const` objects by default](https://github.com/hsutter/cppfront/wiki/Design-note%3A-const-objects-by-default).
inout y : std::string // a string I can read from and write to
49
+
)
50
+
= {
51
+
y = y + to_string(x); // read x, read and write y
52
+
}
53
+
54
+
wrap_f: (
55
+
forward x // a generic value of deduced type I can forward
56
+
) // (omitting x's type means the same as ': _')
57
+
= {
58
+
global_counter += x; // ok to read x
59
+
f(x); // last use: automatically does 'std::forward<T>(x)'
60
+
}
61
+
```
62
+
44
63
45
64
## <aid="return-values"></a> Return values
46
65
@@ -188,7 +207,7 @@ else {
188
207
189
208
**`#!cpp do`** and **`#!cpp while`** are like always in C++, except that `(``)` parentheses around the condition are not required. Instead, `{``}` braces around the loop body *are* required.
190
209
191
-
**`#!cpp for range do (e)`*****statement*** says "for each element in `range`, call it `e` and perform the statement." The loop parameter `(e)` is an ordinary parameter that can be passed isoing any [parameter passing style](#parameters); as always, the default is `in`, which is read-only and expresses a read-only loop. The statement is not required to be enclosed in braces.
210
+
**`#!cpp for range do (e)`*****statement*** says "for each element in `range`, call it `e` and perform the statement." The loop parameter `(e)` is an ordinary parameter that can be passed using any [parameter passing style](#parameters); as always, the default is `in`, which is read-only and expresses a read-only loop. The statement is not required to be enclosed in braces.
192
211
193
212
Every loop can have a `next` clause, that is performed at the end of each loop body execution. This makes it easy to have a counter for any loop, including a range `#!cpp for` loop.
194
213
@@ -247,6 +266,22 @@ if i % 2 == 1 // if i is odd
247
266
// counter: 1, word: [Betty]
248
267
```
249
268
269
+
Here is the equivalent of the Cpp1 code `for ( int i = 0; i < 10; ++i ){ std::cout << i; }`:
270
+
271
+
```cpp title="Equivalent of Cpp1 'for ( int i = 0; i < 10; ++i ){ std::cout << i; }'"
272
+
(copy i := 0)
273
+
while i < 10
274
+
next i++ {
275
+
std::cout << i;
276
+
}
277
+
```
278
+
279
+
Line by line:
280
+
281
+
-`(copy i := 0)`: Any statement can have [statement-local parameters](declarations.md#from-functions-to-local-scopes-and-back-again), and this is declaring `i` as an `int` that's local to the loop. Parameters by default are `const`, and for not-cheap-to-copy types they bind to the original value; so because we want to modify `i` we say `copy` to explicitly declare this is the loop's own mutable scratch variable.
282
+
-`while i < 10`: The termination condition.
283
+
-`next i++`: The end-of-loop-iteration statement. Note `++` is always postfix in Cpp2.
284
+
250
285
251
286
### Loop names, `#!cpp break`, and `#!cpp continue`
0 commit comments