Programming in D – Tutorial and Reference
Ali Çehreli

Other D Resources

Operator Precedence

As we have been using throughout the book, expressions can be chained with more than one operator. For example, the following line contains four expressions chained with three operators:

    a = b + c * d    // three operators: =, +, and *

Operator precedence rules specify the order that the chained operators are executed in and the expressions that they use. Operators are executed according to their precedences: first the higher ones, then the lower ones.

The following is D's operator precedence table. Operators are listed from the highest precedence to the lowest. The ones that are in the same table row have the same precedence. (Line wrapping inside table cells is insignificant; for example, == and !is have the same precedence.) Unless specified otherwise, operators are left-associative.

Some of the terms used in the table are explained later below.

Operators Description Notes
! Template instantiation Cannot be chained
=> Lambda definition Not a real operator; occurs twice in the table; this row is for binding power to the left
. ++ -- ( [ Postfix operators ( and [ must be balanced with ) and ], respectively
^^ Power operator Right-associative
++ -- * + - ! & ~ cast Unary operators
* / % Binary operators
+ - ~ Binary operators
<< >> >>> Bit shift operators
== != > < >= <= !> !< !>= !<= <> !<> <>= !<>= in !in is !is Comparison operators Unordered with respect to bitwise operators, cannot be chained
& Bitwise and Unordered with respect to comparison operators
^ Bitwise xor Unordered with respect to comparison operators
| Bitwise or Unordered with respect to comparison operators
&& Logical and Short-circuit
|| Logical or Short-circuit
?: Ternary operator Right-associative
= -= += = *= %= ^= ^^= ~= <<= >>= >>>= Assignment operators Right-associative
=> Lambda definition Not a real operator; occurs twice in the table; this row is for binding power to the right
, Comma operator Not to be confused with using comma as a separator (e.g. in parameter lists)
.. Number range Not a real operator; hardwired into syntax at specific points
Chaining

Let's consider the line from the beginning of the chapter:

    a = b + c * d

Because binary * has higher precedence than binary +, and binary + has higher precedence than =, that expression is executed as the following parenthesized equivalent:

    a = (b + (c * d))    // first *, then +, then =

As another example, because postfix . has higher precedence than unary *, the following expression would first access member ptr of object o and then dereference it:

    *o.ptr      // ← dereferences pointer member o.ptr
    *(o.ptr)    // ← equivalent of the above
    (*o).ptr    // ← NOT equivalent of the above

Some operators cannot be chained:

    if (a > b == c) {      // ← compilation ERROR
        // ...
    }
Error: found '==' when expecting ')'

The programmer must specify the desired execution order with parentheses:

    if ((a > b) == c) {    // ← compiles
        // ...
    }
Associativity

If two operators have the same precedence, then their associativity determines which operator is executed first: the one on the left or the one on the right.

Most operators are left-associative; the one on the left-hand side is executed first:

    10 - 7 - 3;
    (10 - 7) - 3;    // ← equivalent of the above (== 0)
    10 - (7 - 3);    // ← NOT equivalent of the above (== 6)

Some operators are right-associative; the one on the right-hand side is executed first:

    4 ^^ 3 ^^ 2;
    4 ^^ (3 ^^ 2);    // ← equivalent of the above (== 262144)
    (4 ^^ 3) ^^ 2;    // ← NOT equivalent of the above (== 4096)
Unordered operator groups

Precedence between bitwise operators and logical operators are not specified by the language:

    if (a & b == c) {      // ← compilation ERROR
        // ...
    }
Error: b == c must be parenthesized when next to operator &

The programmer must specify the desired execution order with parentheses:

    if ((a & b) == c) {    // ← compiles
        // ...
    }
The precedence of =>

Although => is not an operator, it takes part in the table twice to specify how it interacts with operators on its left-hand side and right-hand side.

    l = a => a = 1;

Although both sides of => above have an = operator, => has precedence over = on the left hand side so it binds stronger to a as if the programmer wrote the following parentheses:

    l = (a => a = 1);

On the right-hand side, => has lower precedence than =, so the a on the right-hand side binds stronger to = as if the following extra set of parentheses are specified:

    l = (a => (a = 1));

As a result, the lambda expression does not become just a => a but includes the rest of the expression as well: a => a = 1, which means given a, produce a = 1. That lambda is then assigned to the variable l.

Note: This is just an example; otherwise, a = 1 is not a meaningful body for a lambda because the mutation to its parameter a is seemingly lost and the result of calling the lambda is always 1. (The reason I say "seemingly" is that the assignment operator may have been overloaded for a's type and may have side-effects.)

Comma operator

Comma operator is a binary operator. It executes first the left-hand side expression then the right-hand side expression. The values of both expressions are ignored.

    int a = 1;
    foo(), bar(), ++a;

    assert(a == 2);

The comma operator is most commonly used with for loops when the loop iteration involves mutating more than one variable:

    for ({ int i; int j; } i < 10; ++i, ++j) {
        // ...
    }