Skip to content

Commit 8d19818

Browse files
committed
Follow up merge with additional examples, and update highlighted linenos
1 parent f12047a commit 8d19818

File tree

2 files changed

+28
-16
lines changed

2 files changed

+28
-16
lines changed

docs/cpp2/declarations.md

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,44 @@ All Cpp2 declarations are written as **"_name_ `:` _kind_ `=` _statement_"**.
66

77
- The `:` is pronounced **"is a."**
88

9-
- The `=` is pronounced **"defined as."**
9+
- The `=` is pronounced **"defined as."** For the definition of something that will always have the same value, write `==`.
1010

1111
- The _statement_ is typically an expression statement (e.g., `#!cpp a + b();`) or a compound statement (e.g., `#!cpp { /*...*/ return c(d) / e; }`).
1212

1313
- Various parts of the syntax allow a `_` "don't care" wildcard or can be omitted entirely to accept a default (e.g., `#!cpp x: int = 0;` can be equivalently written `#!cpp x: _ = 0;` or `#!cpp x := 0;` both of which deduce the type).
1414

15-
> Note: When the type is omitted, whitespace does not matter, and writing `#!cpp x: = 0;` or `#!cpp x : = 0;` or `#!cpp x := 0;` or other whitespace is just a stylistic choice. This documentation's style uses the last one, except when there are multiple adjacent declaration lines this style lines up their `:` and `=`.
16-
17-
> Note: The only variation to the above is that 'constexpr' functions are written with `==` instead of `=` (e.g., `#!cpp square: (i: int) == i * i;`).
15+
> Notes:
16+
>
17+
> - When the type is omitted, whitespace does not matter, and writing `#!cpp x: = 0;` or `#!cpp x : = 0;` or `#!cpp x := 0;` or other whitespace is just a stylistic choice. This documentation's style uses the last one, except when there are multiple adjacent declaration lines this style lines up their `:` and `=`.
18+
>
19+
> - `==` stresses that this name will always have the given value, to express [aliases](./aliases.md) and side-effect-free 'constexpr' functions (e.g., `#!cpp square: (i: int) == i * i;`).
1820
1921
## Examples
2022

21-
``` cpp title="Consistent declarations — name : kind = statement" hl_lines="2 5 10 17 21 25 34 40"
23+
``` cpp title="Consistent declarations — name : kind = statement" linenums="1" hl_lines="2 6 10 15 24 28 32 43 49 53"
2224
// n is a namespace defined as the following scope
2325
n: namespace
2426
= {
25-
// shape is a type defined as the following scope
26-
shape: type
27+
// shape is a templated type with one type parameter T
28+
// (equivalent to '<T: type>') defined as the following scope
29+
shape: <T> type
2730
= {
28-
// points is an object of type std::vector<point2d>,
31+
// point is a type defined as being always the same as
32+
// (i.e., an alias for) T
33+
point_type: type == T;
34+
35+
// points is an object of type std::vector<point_type>,
2936
// defined as having an empty default value
3037
// (type-scope objects are private by default)
31-
points: std::vector<point2d> = ();
38+
points: std::vector<point_type> = ();
3239

3340
// draw is a function taking 'this' and 'canvas' parameters
3441
// and returning bool, defined as the following body
3542
// (type-scope functions are public by default)
36-
// - this is as if 'this: shape', an object of type shape
37-
// - where is an object of type canvas
43+
//
44+
// this is an object of type shape (as if written 'this: shape')
45+
//
46+
// where is an object of type canvas
3847
draw: (this, where: canvas) -> bool
3948
= {
4049
// pen is an object of deduced (omitted) type 'color',
@@ -52,20 +61,23 @@ n: namespace
5261

5362
// count is a function taking 'this' and returning a type
5463
// deduced from its body, defined as a single-expression body
64+
// (equivalent to '= { return points.ssize(); }' but omitting
65+
// syntax where we're using the language defaults)
5566
count: (this) = points.ssize();
5667

5768
// ...
5869
}
5970

60-
// color is an @enum type (see Note)
71+
// color is an @enum type (see Note) defined as having these enumerators
6172
color: @enum type = { red; green; blue; }
6273

63-
// a constexpr function is defined with `==`
64-
calc_next_year: (year: i32) -> i32 == { return year + 1; }
74+
// calc_next_year is a function defined as always returning the same
75+
// value for the same input (i.e., 'constexpr', side effect-free)
76+
calc_next_year: (year: i32) -> i32 == year + 1;
6577
}
6678
```
6779

68-
> Note: `@enum` is a metafunction, which provides an easy way to opt into a group of defaults, constraints, and generated functions. For details, see [`@enum`, `@flag_enum`](metafunctions.md#enum-flag_enum)
80+
> Note: `@enum` is a metafunction, which provides an easy way to opt into a group of defaults, constraints, and generated functions. For details, see [`@enum`](metafunctions.md#enum).
6981
7082

7183
## <a id="requires"></a> `requires` constraints

docs/cpp2/types.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ For example:
251251

252252
``` cpp title="Writing the <=> operator" hl_lines="5-7 13"
253253
item: type = {
254-
x: i32 = 0;
254+
x: i32 = ();
255255
y: std::string = ();
256256

257257
operator<=>: (this, that) -> std::strong_ordering;

0 commit comments

Comments
 (0)