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
Copy file name to clipboardExpand all lines: docs/index.md
+3-3Lines changed: 3 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -17,7 +17,7 @@ We can't make an improvement that large to C++ via gradual evolution to today's
17
17
18
18
-**Freedom to make any desired improvement, without breaking any of today's code.** Cpp2 is designed to take all the consensus C++ best-practices guidance we already teach, and make it the default when using "syntax 2." Examples: Writing unsafe type casts is just not possible in Cpp2 syntax; and Cpp2 can change language defaults to make them simpler and safer. You can always "break the glass" when needed to violate the guidance, but has to opt out explicitly to write unsafe code, so if the program has a bug you can grep for those places to look at first. For details, see [Design note: unsafe code](https://github.com/hsutter/cppfront/wiki/Design-note%3A-Unsafe-code).
19
19
20
-
-**Perfect link compatibility always on, perfect source compatibility always available (but you pay for it only if you use it).** Any type/function/object written in either syntax is always still just a normal C++ type/function/object, so any code or library written in either Cpp2 or today's C++ syntax ("Cpp1" for short) can seamlessly call each other, with no wrapping/marshaling/thunking. You can write a "mixed" source files that has both Cpp2 and Cpp1 code and get perfect backward C++ source compatibility (even SFINAE and macros), or you can write a "pure" all-Cpp2 source file and write code in a 10x simpler syntax.
20
+
-**Perfect link compatibility always on, perfect source compatibility always available (but you pay for it only if you use it).** Any type/function/object/namespace/module/etc. written in either syntax is always still just a normal C++ type/function/object/namespace/module/etc., so any code or library written in either Cpp2 or today's C++ syntax ("Cpp1" for short) can seamlessly call each other, with no wrapping/marshaling/thunking. You can write a "mixed" source files that has both Cpp2 and Cpp1 code and get perfect backward C++ source compatibility (even SFINAE and macros), or you can write a "pure" all-Cpp2 source file and write code in a 10x simpler syntax.
21
21
22
22
**What it isn't.** Cpp2 is not a successor or alternate language with it own divergent or incompatible ecosystem. For example, it does not have its own nonstandard incompatible modules/concepts/etc. that compete with the Standard C++ features; and does not replace your Standard C++ compiler and other tools.
This short program code already illustrates a few Cpp2 essentials.
81
81
82
-
**Consistent context-free syntax.** Cpp2 is designed so that there is one general way to spell a given thing, that works consistently everywhere. All Cpp2 type/function/object declarations use the unambiguous and context-free syntax **"_name_`:`_kind_`=`_statement_"**. The `:` is pronounced **"is a,"** and the `=` is pronounced is pronounced **"defined as."**
82
+
**Consistent context-free syntax.** Cpp2 is designed so that there is one general way to spell a given thing, that works consistently everywhere. All Cpp2 type/function/object/namespace/module/etc. declarations use the unambiguous and context-free syntax **"_name_`:`_kind_`=`_statement_"**. The `:` is pronounced **"is a,"** and the `=` is pronounced is pronounced **"defined as."**
83
83
84
84
-`main`**is a** function that takes no arguments and returns nothing, and is **defined as** the code body shown.
85
85
@@ -236,7 +236,7 @@ That's enough to enable builds, and the IDE just picks up the rest from the `.cp
236
236
237
237
-**The `#line` directives cppfront emits in the generated `.cpp` file.** Most C++ debuggers recognize these and will know to step through the `.cpp2` file. Note that `#line` emission is on by default, but if you choose `-c` (short for `-clean-cpp1`) these will be suppressed and then the debugger will step through the generated C++ code instead.
238
238
239
-
-**Regardless of syntax, every type/function/object is still just an ordinary C++ type/function/object.** Most C++ debugger visualizers will just work and show beautiful output for the types your program uses, including to use any in-the-box visualizers for all the `std::` types (since those are used directly as usual) and any custom visualizers you may have already written for your own types or popular library types.
239
+
-**Regardless of syntax, every type/function/object/namespace/module/etc. is still just an ordinary C++ type/function/object/namespace/module/etc.** Most C++ debugger visualizers will just work and show beautiful output for the types your program uses, including to use any in-the-box visualizers for all the `std::` types (since those are used directly as usual) and any custom visualizers you may have already written for your own types or popular library types.
Copy file name to clipboardExpand all lines: docs/reference-cpp2.md
+75-8Lines changed: 75 additions & 8 deletions
Original file line number
Diff line number
Diff line change
@@ -17,6 +17,60 @@ The usual `// line comments` and `/* stream comments */` are supported. For exma
17
17
*/
18
18
```
19
19
20
+
### Declarations
21
+
22
+
All Cpp2 declarations are written as **"_name_`:`_kind_`=`_statement_"**.
23
+
24
+
- The `:` is pronounced **"is a."**
25
+
- The `=` is pronounced is pronounced **"defined as."**
26
+
- The _statement_ is typically an expression statement (e.g., `a + b();`) or a compound statement (e.g., `{ /*...*/ return c(d) / e; }`).
27
+
28
+
For example:
29
+
30
+
```cpp title="Example: Consistent declartions — name : kind = statement"
31
+
// n is a namespace defined as the following scope
32
+
n: namespace
33
+
= {
34
+
// shape is a type defined as the following scope
35
+
shape: type
36
+
= {
37
+
// points is an object of type std::vector<point2d>,
38
+
// defined as having an empty default value
39
+
// (type-scope objects are private by default)
40
+
points: std::vector<point2d> = ();
41
+
42
+
// draw is a function taking 'this' and 'canvas' parameters
43
+
// and returning bool, defined as the following body
44
+
// (type-scope functions are public by default)
45
+
// - this is as if 'this: shape', an object of type shape
46
+
// - where is an object of type canvas
47
+
draw: (this, where: canvas) -> bool
48
+
= {
49
+
// pen is an object of deduced (omitted) type 'color',
50
+
// defined as having initial value 'color::red'
51
+
pen := color::red;
52
+
53
+
// success is an object of deduced (omitted) type bool,
54
+
// defined as having initial value 'false'
55
+
success := false;
56
+
57
+
// ...
58
+
59
+
return success;
60
+
}
61
+
62
+
// count is a function taking 'this' and returning a type
63
+
// deduced from its body, defined as a single-expression body
64
+
count: (this) = points.ssize();
65
+
66
+
// ...
67
+
}
68
+
69
+
// color is an @enum type (described later)
70
+
color: @enum type = { red; green; blue; }
71
+
}
72
+
```
73
+
20
74
### The `_` wildcard, including explicit discard
21
75
22
76
`_` is pronounced **"don't care"** and allowed as a wildcard in most contexts. For example:
@@ -237,9 +291,16 @@ test: (x) = {
237
291
}
238
292
```
239
293
240
-
### `inspect` expressions — pattern matching
294
+
### `inspect` — pattern matching
295
+
296
+
An `inspect expr -> Type` expression allows pattern matching using `is`.
241
297
242
-
An `inspect` expression allows pattern matching using `is`. For example:
298
+
-`expr` is evaluated once.
299
+
- Each alternative spelled `is C` is evaluated in order as if called with `expr is C`.
300
+
- If an alternative evaluates to `true`, then its `= 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 `:Type = alternative;` — i.e., an unnamed object expression (aka 'temporary object') of type `Type` initialized with `alternative`.
301
+
- A catchall `is _` is required.
302
+
303
+
For example:
243
304
244
305
```cpp title="Example: Using inspect"
245
306
// A generic function that takes an argument 'x' of any type
@@ -248,19 +309,25 @@ test: (x) = {
248
309
forty_two := 42;
249
310
std::cout << inspect x -> std::string {
250
311
is 0 = "zero"; // == 0
251
-
is (forty_two) = "the answer"; // == forty_two
252
-
is int = "integer"; // is an int with some other value
253
-
is std::string = x as std::string; // is a std::string
254
-
is std::vector = "a std::vector"; // is a vector</*some-type*/>
255
-
is _ = "(no match)"; // something else
312
+
is (forty_two) = "the answer"; // == 42
313
+
is int = "integer"; // is type int (value other than 0 or 42)
314
+
is std::string = x as std::string; // is type std::string
315
+
is std::vector = "a std::vector"; // is a vector</*of-some-type*/>
316
+
is _ = "(no match)"; // is something else
256
317
} << "\n";
257
318
}
319
+
320
+
// Sample call site
321
+
test(42);
322
+
// Behaves as if the following function were called:
0 commit comments