Type operators

Table of contents

Overview

Carbon provides the following operators to transform types:

  • const as a prefix unary operator produces a const-qualified type.
  • * as a postfix unary operator produces a pointer type to some other type.

The pointer type operator is also covered as one of the pointer operators.

Details

The semantic details of both const-qualified types and pointer types are provided as part of the values design:

The syntax of these operators tries to mimic the most common appearance of const types and pointer types in C++.

Precedence

Because these are type operators, they don’t have many precedence relationship with non-type operators.

  • const binds more tightly than * and can appear unparenthesized in an operand, despite being both a unary operator and having whitespace separating it.
    • This allows the syntax of a pointer to a const i32 to be const i32*, which is intended to be familiar to C++ developers.
    • Forming a const pointer type requires parentheses: const (i32*).
  • All type operators bind more tightly than as so they can be used in its type operand.
    • This also allows a desirable transitive precedence with if: if condition then T* else U*.

Alternatives considered

References