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/cpp2/declarations.md
+19Lines changed: 19 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -6,6 +6,8 @@ All Cpp2 declarations are written as **"_name_ `:` _kind_ `=` _statement_"**.
6
6
7
7
- The `:` is pronounced **"is a."**
8
8
9
+
- The _kind_ can start with [template parameters](#template-parameters).
10
+
9
11
- The `=` is pronounced **"defined as."** For the definition of something that will always have the same value, write `==`.
10
12
11
13
- The _statement_ is typically an expression statement (e.g., `#!cpp a + b();`) or a compound statement (e.g., `#!cpp { /*...*/ return c(d) / e; }`).
@@ -18,6 +20,23 @@ All Cpp2 declarations are written as **"_name_ `:` _kind_ `=` _statement_"**.
18
20
>
19
21
> -`==` 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;`).
A template parameter list is enclosed by `<``>` angle brackets, and the parameters separated by commas. Each parameter is declared using the [same syntax as any type or object](declarations.md). If a parameter's **`:`*****kind*** is not specified, the default is `: type`.
Copy file name to clipboardExpand all lines: docs/cpp2/functions.md
+150-6Lines changed: 150 additions & 6 deletions
Original file line number
Diff line number
Diff line change
@@ -3,11 +3,30 @@
3
3
4
4
## Overview
5
5
6
-
TODO
6
+
A function is defined by writing a function signature after the `:` and a statement (expression or `{``}` compound statement) after the `=`. After the optional [template parameters](declarations.md#template-parameters) available for all declarations, a function signatures consists of a possibly-empty [parameter list](#parameters), and an optional function [return values](#return-values).
7
+
8
+
For example, the minimal function named `func` that takes no parameters and returns nothing (`#!cpp void`) is:
9
+
10
+
```cpp title="A minimal function"
11
+
func: ( /* no parameters */ ) = { /* empty body */ }
12
+
```
7
13
8
14
9
15
## <a id="parameters"></a> Parameters
10
16
17
+
The parameter list is enclosed by `(` `)` parentheses, and the parameters separated by commas. Each parameter is declared using the [same syntax as any object](declarations.md). For example:
z: std::map<i32, std::string> // parameter z is a std::map
24
+
)
25
+
= {
26
+
// ...
27
+
}
28
+
```
29
+
11
30
There are six ways to pass parameters that cover all use cases:
12
31
13
32
| Parameter ***kind***| "Pass me an `x` I can ______" | Accepts arguments that are | Special semantics |***kind***`x: X` Compiles to Cpp1 as |
@@ -25,7 +44,58 @@ There are six ways to pass parameters that cover all use cases:
25
44
26
45
## <aid="return-values"></a> Return values
27
46
28
-
TODO
47
+
A function can return either of the following. The default is `#!cpp -> void`.
48
+
49
+
(1) **`#!cpp -> X`** to return a single unnamed value of type `X`, which can be `#!cpp void` to signify the function has no return value. If `X` is not `#!cpp void`, the function body must have a `#!cpp return /*value*/;` statement that returns a value of type `X` on every path that exits the function. For example:
50
+
51
+
```cpp title="Functions with an unnamed return value" hl_lines="2 4 7 9 12 14"
// Or, using syntactic defaults, the following has identical meaning:
65
+
add: (a, b) -> _ = a+b;
66
+
```
67
+
68
+
(2) **`#!cpp -> ( /* parameter list */ )`** to return a list of named return parameters using the same [parameters](#parameters) syntax, but where the only passing styles are `out` (the default, which moves where possible) or `forward`. The function body must [initialize](objects.md#init) the value of each return-parameter `ret` in its body the same way as any other local variable. An explicit return statement is written just `#!cpp return;` and returns the named values; the function has an implicit `#!cpp return;` at the end. For example:
`if` and `else` are like always in C++, except that `(``)` parentheses around the condition are not required. Instead, `{``}` braces around a branch body *are* required. For example:
**`#!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.
168
+
169
+
**`#!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.
170
+
171
+
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.
172
+
173
+
> Note: Whitespace is just a stylistic choice. This documentation's style generally puts each keyword on its own line and lines up what follows.
while i < words.ssize() // while this condition is true
182
+
next i++ // and increment i after each loop body is run
183
+
{ // do this loop body
184
+
std::cout << "word: (words[i])$\n";
185
+
}
186
+
// prints:
187
+
// word: Adam
188
+
// word: Betty
189
+
190
+
do { // do this loop body
191
+
std::cout << "**\n";
192
+
}
193
+
next i-- // and decrement i after each loop body is run
194
+
while i > 0; // while this condition is true
195
+
// prints:
196
+
// **
197
+
// **
198
+
199
+
for words // for each element in 'words'
200
+
next i++ // and increment i after each loop body is run
201
+
do (inout word) // declare via 'inout' the loop can change the contents
202
+
{ // do this loop body
203
+
word = "[" + word + "]";
204
+
std::cout << "counter: (i)$, word: (word)$\n";
205
+
}
206
+
// prints:
207
+
// counter: 0, word: [Adam]
208
+
// counter: 1, word: [Betty]
209
+
```
210
+
211
+
There is no special "select" or "where" to perform the loop body for only a subset of matches, because this can naturally be expressed with `if`. For example:
212
+
213
+
```cpp title="Using loops + if" hl_lines="7"
214
+
// Continuing the previous example
215
+
i = 0;
216
+
217
+
for words
218
+
next i++
219
+
do (word)
220
+
if i % 2 == 1// if i is odd
221
+
{ // do this loop body
222
+
std::cout << "counter: (i)$, word: (word)$\n";
223
+
}
224
+
// prints:
225
+
// counter: 1, word: [Betty]
226
+
```
227
+
228
+
229
+
### Loop names, `#!cpp break`, and `#!cpp continue`
86
230
87
231
Loops can be named using the usual **name `:`** syntax that introduces all names, and `#!cpp break` and `#!cpp continue` can refer to those names. For example:
88
232
89
-
```cpp title="Using named break and continue" hl_lines="6 10"
233
+
```cpp title="Using named break and continue" hl_lines="1 3 6 10"
Copy file name to clipboardExpand all lines: docs/cpp2/types.md
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -268,7 +268,7 @@ test: (x: item, y: item) = {
268
268
}
269
269
```
270
270
271
-
The above is the same as in Cpp1 because most of Cpp2's `#!cpp operator<=>` feature has already been merged into ISO C++ (Cpp1). In additiona, in Cpp2 comparisons with the same precedence can be safely chained, and always have the mathematically sound transitive meaning or else are rejected at compile time:
271
+
The above is the same as in Cpp1 because most of Cpp2's `#!cpp operator<=>` feature has already been merged into ISO C++ (Cpp1). In addition, in Cpp2 comparisons with the same precedence can be safely chained, and always have the mathematically sound transitive meaning or else are rejected at compile time:
272
272
273
273
-**Valid chains: All `<`/`<=`, all `>`/`>=`, or all `==`.** All mathematically sound and safe chains like `a <= b < c` are supported, with efficient single evaluation of each term. They are "sound" because they are transitive; these chains imply a relationship between `a` and `c` (in this case, the chain implies that `a <= c` is also true).
Copy file name to clipboardExpand all lines: docs/index.md
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -9,7 +9,7 @@ main: () = {
9
9
10
10
## <aid="what-is-cpp2"></a> What is Cpp2?
11
11
12
-
"Cpp2", short for "C++ syntax 2", is my ([Herb Sutter's](https://github.com/hsutter)) personal project to try to make writing ordinary C++ types/functions/objects be much **simpler and safer**, without breaking backward compatibility. Bjarne Stroustrup said it best:
12
+
"Cpp2," short for "C++ syntax 2," is my ([Herb Sutter's](https://github.com/hsutter)) personal project to try to make writing ordinary C++ types/functions/objects be much **simpler and safer**, without breaking backward compatibility. Bjarne Stroustrup said it best:
13
13
14
14
> "Inside C++, there is a much smaller and cleaner language struggling to get out." <br>  — Bjarne Stroustrup, _The Design and Evolution of C++_ (D&E), 1994
0 commit comments