Programming in D – Tutorial and Reference
Ali Çehreli

Other D Resources

Ternary Operator ?:

The ?: operator works very similarly to an if-else statement:

    if (/* condition check */) {
        /* ... expression(s) to execute if true */

    } else {
        /* ... expression(s) to execute if false */
    }

The if statement executes either the block for the case of true or the block for the case of false. As you remember, being a statement, it does not have a value; if merely affects the execution of code blocks.

On the other hand, the ?: operator is an expression. In addition to working similary to the if-else statement, it produces a value. The equivalent of the above code is the following:

/* condition */ ? /* truth expression */ : /* falsity expression */

Because it uses three expressions, the ?: operator is called the ternary operator.

The value that is produced by this operator is either the value of the truth expression or the value of the falsity expression. Because it is an expression, it can be used anywhere that expressions can be used.

The following examples contrast the ?: operator to the if-else statement. The ternary operator is more concise for the cases that are similar to these examples.

As can be seen from the examples above, the code is more concise and clearer with the ternary operator in certain situations.

The type of the ternary expression

The value of the ?: operator is either the value of the truth expression or the value of the falsity expression. The types of these two expressions need not be the same but they must have a common type.

The common type of two expressions is decided by a relatively complicated algorithm, involving type conversions and inheritance. Additionally, depending on the expressions, the kind of the result is either an lvalue or an rvalue. We will see these concepts in later chapters.

For now, accept common type as a type that can represent both of the values without requiring an explicit cast. For example, the integer types int and long have a common type because they can both be represented as long. On the other hand, int and string do not have a common type because neither int nor string can automatically be converted to the other type.

Remember that a simple way of determining the type of an expression is using typeof and then printing its .stringof property:

    int i;
    double d;

    auto result = someCondition ? i : d;
    writeln(typeof(result).stringof);

Because double can represent int but not the other way around, the common type of the ternary expression above is double:

double

To see an example of two expressions that do not have a common type, let's look at composing a message that reports the number of items to be shipped. Let's print "A dozen" when the value equals 12: "A dozen items will be shipped." Otherwise, let's have the message include the exact number: "3 items will be shipped."

One might think that the varying part of the message can be selected with the ?: operator:

    writeln(
        (count == 12) ? "A dozen" : count, // ← compilation ERROR
        " items will be shipped.");

Unfortunately, the expressions do not have a common type because the type of "A dozen" is string and the type of count is int.

A solution is to first convert count to string. The function to!string from the std.conv module produces a string value from the specified parameter:

import std.conv;
// ...
    writeln((count == 12) ? "A dozen" : to!string(count),
            " items will be shipped.");

Now, as both of the selection expressions of the ?: operator are of string type, the code compiles and prints the expected message.

Exercise

Have the program read a single int value as the net amount where a positive value represents a gain and a negative value represents a loss.

The program should print a message that contains "gained" or "lost" depending on whether the amount is positive or negative. For example, "$100 lost" or "$70 gained". Even though it may be more suitable, do not use the if statement in this exercise.