Declarations and aliases
Unified declarations
All Cpp2 declarations are written as "name : kind = statement".
-
The name must be a valid identifier (start with a letter, and consist of letters, digits, or
_). The name can be variadic (be a name for a list of zero or more things) by writing a...suffix at the end of the name. -
The
:is pronounced "is a." -
The kind can start with template parameters and end with
requiresconstraints. -
The
=is pronounced "defined as." For the definition of something that will always have the same value, write==, pronounced "defined as a synonym for". -
The statement is typically an expression statement (e.g.,
a + b();) or a compound statement (e.g.,{ /*...*/ return c(d) / e; }).
Various parts of the syntax allow a _ "don't care" wildcard or can be omitted entirely to accept a default (e.g., x: int = 0; can be equivalently written x: _ = 0; or x := 0; both of which deduce the type).
Notes:
When the type is omitted, whitespace does not matter, and writing
x: = 0;orx : = 0;orx := 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=.
==stresses that this name will always have the given value, to express aliases and side-effect-free 'constexpr' function aliases.
Unnamed declaration expressions
In an expression, most declarations can be written without a name (just starting with :). Such unnamed declaration expressions are useful for single-use temporary variables or 'lambda' functions that don't need a name to be reused elsewhere. For example:
-
:widget = 42is an unnamed expression-local (aka temporary) object of typewidgetdefined as having the initial value42. It uses the same general syntax, just without declaring a name. -
:(x) = std::cout << xis an unnamed expression-local generic function expression (aka lambda) defined as having the given one-statement body. The body can include captures.
Both just omit the name and make the final ; optional. Otherwise, they have the identical syntax and meaning as if you declared the same thing with a name outside expression scope (e.g., w: widget = 42; or f: (x) = std::cout << x;) and then used the name in the expression.
Note: Throughout Cpp2, every declaration is written with
:, and every use of:is a declaration.
From functions to local scopes, and back again
The function syntax is deliberately designed to be general, so you can omit parts. This means Cpp2 has no special "lambda function" syntax for unnamed functions; an unnamed function is really an unnamed function, written using the ordinary function just without a name. This scales all the way down to ordinary blocks and statements, which are written the same as functions that have no name or parameters.
We can illustrate this in two directions. First, let's start with a full function, and successively omit optional parts that we aren't currently using:
// Full named function
f:(x: int = init) = { /*...*/ } // x is a parameter to the function
f:(x: int = init) = statement; // same, except return type is deduced
// Omit name => anonymous function (aka 'lambda')
:(x: int = init) = { /*...*/ } // x is a parameter to the function
:(x: int = init) = statement; // same, except return type is deduced
// Omit declaration => local and immediate (aka 'let' in other languages)
(x: int = init) { /*...*/ } // x is a parameter to this
(x: int = init) statement; // compound or single-statement
// Omit parameters => ordinary block or statement
{ /*...*/ } // ordinary compound statement
statement; // ordinary statement
Conversely, we can start with an ordinary block or statement, and successively build it up to make it more powerful:
// Ordinary block or statement
{ /*...*/ } // ordinary compound statement
statement; // ordinary statement
// Add parameters => more RAII locally-scoped variables
(x: int = init) { /*...*/ } // x is destroyed after this
(x: int = init) statement; // compound or single-statement
// Add declaration => treat the code as a callable object
:(x: int = init) = { /*...*/ } // x is a parameter to the function
:(x: int = init) = statement; // same, except return type is deduced
// Add name => full named function
f:(x: int = init) = { /*...*/ } // x is a parameter to the function
f:(x: int = init) = statement; // same, except return type is deduced
Template parameters
A template parameter list is a list enclosed by < > angle brackets, and the parameters separated by commas. Each parameter is declared using the same syntax as any type or object. If a parameter's : kind is not specified, the default is : type.
For example: