Skip to content

Functions

Overview

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 available for all declarations, a function signature consists of a possibly-empty parameter list, and one or more optional return values.

For example, the minimal function named func that takes no parameters and returns nothing (void) is:

A minimal function
func: ( /* no parameters */ ) = { /* empty body */ }

Function signatures: Parameters, returns, and using function types

Overview

There are six kinds of function parameters, and two of them are the kinds of functions returns:

Kind Parameter Return
in ⭐
inout ✅
out ✅
copy ✅
move ✅ ✅
forward ✅ ⭐

The two cases marked ⭐ can automatically pass/return by value or by reference, and so they can be optionally written with _ref to require pass/return by reference and not by value (i.e., in_ref, -> forward_ref).

That's it. For details, see below.

Parameters

The parameter list is a list enclosed by ( ) parentheses. Each parameter is declared using the same unified syntax as used for all declarations. For example:

Declaring parameters
func: (
    x: i32,                         // parameter x is a 32-bit int
    y: std::string,                 // parameter y is a std::string
    z: std::map<i32, std::string>   // parameter z is a std::map
    )
= {
    // ...
}

The parameter type can be deduced by writing _ (the default, so it can be omitted). You can use is to declare a type constraint (e.g., a concept) that a deduced type must match, in which case _ is required. For example:

Declaring a parameter of constrained deduced type
//  ordinary generic function, x's type is deduced
print: (x: _) = { std::cout << x; }
print: (x)    = { std::cout << x; } // same, using the _ default

//  number's type is deduced, but must match the std::integral concept
calc: (number: _ is std::integral) = { /*...*/ }

There are six ways to pass parameters that cover all use cases, that can be written before the parameter name:

Parameter kind "Pass an x the function ______" Accepts arguments that are Special semantics kind x: X compiles to Cpp1 as
in (default) can read from anything always const

automatically passes by value if cheaply copyable

to guarantee a by-reference passing, use in_ref

X const x or
X const& x
copy gets a copy of anything acts like a normal local variable initialized with the argument X x
inout can read from and write to lvalues X& x
out writes to (including construct) lvalues (including uninitialized) must = assign/construct before other uses cpp2::impl::out<X>
move moves from (consume the value of) rvalues automatically moves from every definite last use X&&
forward forwards anything automatically forwards from every definite last use auto&&, and if a specific type is named also a requires-constraint requiring convertibilty to that type

Note: All parameters and other objects in Cpp2 are const by default, except for local variables. For details, see Design note: const objects by default.

For example:

Declaring parameter kinds
append_x_to_y: (
    x       : i32,          // an i32 I can read from (i.e., const)
    inout y : std::string   // a string I can read from and write to
    )
= {
    y = y + to_string(