Skip to content

Commit 039da01

Browse files
committed
Update docs to reflect = function change and @hashable
1 parent 2992560 commit 039da01

File tree

6 files changed

+25
-18
lines changed

6 files changed

+25
-18
lines changed

docs/cpp2/common.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ All lists use `,` commas between list items, and may be enclosed by
6868
For example:
6969

7070
``` cpp title="Lists" hl_lines="1 4 6 7"
71-
print: <T,U> (t: T, u: U) = std::cout << t << u << "\n";
71+
print: <T,U> (t: T, u: U) = { std::cout << t << u << "\n"; }
7272

7373
main: () = {
7474
array: std::array = ('A', 'B', 'C');
@@ -89,7 +89,7 @@ An extra comma at the end of the list, before the closing `)` or `>`, is always
8989
For example:
9090

9191
``` cpp title="Lists, using optional trailing commas just because we can" hl_lines="1 4 6 7"
92-
print: <T,U,> (t: T, u: U,) = std::cout << t << u << "\n";
92+
print: <T,U,> (t: T, u: U,) = { std::cout << t << u << "\n"; }
9393

9494
main: () = {
9595
array: std::array = ('A', 'B', 'C',);

docs/cpp2/declarations.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@ We can illustrate this in two directions. First, let's start with a full functio
4646
``` cpp title="Start with a full function, and successively omit optional parts if unused" hl_lines="1 5 9 13"
4747
// Full named function
4848
f:(x: int = init) = { /*...*/ } // x is a parameter to the function
49-
f:(x: int = init) = statement; // same, { } is implicit
49+
f:(x: int = init) = statement; // same, except return type is deduced
5050

5151
// Omit name => anonymous function (aka 'lambda')
5252
:(x: int = init) = { /*...*/ } // x is a parameter to the function
53-
:(x: int = init) = statement; // same, { } is implicit
53+
:(x: int = init) = statement; // same, except return type is deduced
5454

5555
// Omit declaration => local and immediate (aka 'let' in other languages)
5656
(x: int = init) { /*...*/ } // x is a parameter to this
@@ -74,11 +74,11 @@ Conversely, we can start with an ordinary block or statement, and successively b
7474
7575
// Add declaration => treat the code as a callable object
7676
:(x: int = init) = { /*...*/ } // x is a parameter to the function
77-
:(x: int = init) = statement; // same, { } is implicit
77+
:(x: int = init) = statement; // same, except return type is deduced
7878
7979
// Add name => full named function
8080
f:(x: int = init) = { /*...*/ } // x is a parameter to the function
81-
f:(x: int = init) = statement; // same, { } is implicit
81+
f:(x: int = init) = statement; // same, except return type is deduced
8282
8383
```
8484

@@ -166,7 +166,7 @@ n: namespace
166166
// deduced from its body, defined as a single-expression body
167167
// (equivalent to '= { return points.ssize(); }' but omitting
168168
// syntax where we're using the language defaults)
169-
count: (this) = points.ssize();
169+
count: (this) -> _ = points.ssize();
170170

171171
// ...
172172
}

docs/cpp2/expressions.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ For example:
1818
``` cpp title="Function calls" hl_lines="3 7 11 16 19 20"
1919
// Generic function to log something
2020
// This calls operator<< using operator notation
21-
log: (x) = clog << x;
21+
log: (x) = { clog << x; }
2222

2323
f: ( v : std::vector<widget> ) = {
2424
// This calls log() with the result of std::vector::size()

docs/cpp2/functions.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ For example:
113113
// A function returning no value (void)
114114
increment_in_place: (inout a: i32) -> void = { a++; }
115115
// Or, using syntactic defaults, the following has identical meaning:
116-
increment_in_place: (inout a: i32) = a++;
116+
increment_in_place: (inout a: i32) = { a++; }
117117

118118
// A function returning a single value of type i32
119119
add_one: (a: i32) -> i32 = { return a+1; }
@@ -127,11 +127,11 @@ add: (a, b) -> forward _ = a+b;
127127
add: (a, b) a+b;
128128

129129
// A generic function expression returning a single value of deduced type
130-
vec.std::ranges::sort( :(x:_, y:_) -> _ = { return y<x; } );
130+
vec.std::ranges::sort( :(x:_, y:_) -> forward _ = { return y<x; } );
131131
// Or, using syntactic defaults, the following has identical meaning:
132-
vec.std::ranges::sort( :(x,y) y<x );
132+
vec.std::ranges::sort( :(x,y) = y<x );
133133
// Both are identical to this, which uses the most verbose possible syntax:
134-
vec.std::ranges::sort( :<T:type, U:type> (x:T, y:U) -> _ = { return y<x; } );
134+
vec.std::ranges::sort( :<X:type, Y:type> (x:X, y:Y) -> forward _ = { return y<x; } );
135135
```
136136
137137
#### Return parameter lists: Nameable return value(s)
@@ -466,32 +466,32 @@ Next, `in` is the default [parameter kind](#parameters). So we can use that defa
466466
equals: (a, b) -> _ = { return a == b; }
467467
```
468468
469-
We already saw that `{` `}` is the default for a single-line function that returns nothing. Similarly, `{ return` and `}` is the default for a single-line function that returns something:
469+
We already saw that `{ return` ... `; }` is the default for a single-expression function body that deduces its return type:
470470
471471
``` cpp title="equals: Identical meaning, now using the { return ... } default body decoration"
472472
equals: (a, b) -> _ = a == b;
473473
```
474474

475-
Next, `#!cpp -> _ =` (deduced return type) is the default for single-expression functions that return something and so can be omitted:
475+
Next, `#!cpp -> forward _` (fully deduced return type) is the default for single-expression functions that return something, and in this case will have the same meaning as `#!cpp -> _` :
476476

477477
``` cpp title="equals: Identical meaning, now using the -> _ = default for functions that return something"
478-
equals: (a, b) a == b;
478+
equals: (a, b) = a == b;
479479
```
480480

481481
Finally, at expression scope (aka "lambda/temporary") functions/objects aren't named, and the trailing `;` is optional:
482482

483483
``` cpp title="(not) 'equals': Identical meaning, but without a name as an unnamed function at expression scope"
484-
:(a, b) a == b
484+
:(a, b) = a == b
485485
```
486486

487487
Here are some additional examples of unnamed function expressions:
488488

489489
``` cpp title="Some more examples of unnamed function expressions"
490490
std::ranges::for_each( a, :(x) = std::cout << x );
491491

492-
std::ranges::transform( a, b, :(x) x+1 );
492+
std::ranges::transform( a, std::back_inserter(b), :(x) = x+1 );
493493

494-
where_is = std::ranges::find_if( a, :(x) x == waldo$ );
494+
where_is = std::ranges::find_if( b, :(x) = x == waldo$ );
495495
```
496496
497497
> Note: Cpp2 doesn't have a separate "lambda" syntax; you just use the regular function syntax at expression scope to write an unnamed function, and the syntactic defaults are chosen to make such function expressions convenient to write. And because in Cpp2 every local variable [capture](expressions.md#captures) (for example, `waldo$` above) is written in the body, it doesn't affect the function syntax.

docs/cpp2/metafunctions.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,11 @@ A `struct` is a type with only public bases, objects, and functions, with no vir
157157
- there is a user-written `operator=`
158158

159159

160+
#### `hashable`
161+
162+
A `hashable` type provides a `hash: (this) -> size_t` function that performs a memberwise hash of its data members using `std::hash`.
163+
164+
160165
### For polymorphic types (interfaces, base classes)
161166

162167

docs/cpp2/types.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,8 @@ The above is the same as in Cpp1 because most of Cpp2's `#!cpp operator<=>` feat
281281
if lo <= requested < hi {
282282
// ... do something ...
283283
}
284+
285+
// Equivalent result to: (lo ..< hi).contains( requested )
284286
```
285287

286288
For more details, see [P0515R0 "Consistent comparison" section 3.3](https://wg21.link/p0515r0) and [P0893 "Chaining comparisons"](https://wg21.link/p0893).

0 commit comments

Comments
 (0)