Programming in D – Tutorial and Reference
Ali Çehreli

Other D Resources

The null Value and the is Operator

We saw in earlier chapters that a variable of a reference type needs not reference a particular object:

    MyClass referencesAnObject = new MyClass;

    MyClass variable;   // does not reference an object

Being a reference type, variable above does have an identity but it does not reference any object yet. Such an object can be imagined to have a place in memory as in the following picture:

      │ null │

A reference that does not reference any value is null. We will expand on this below.

Such a variable is in an almost useless state. Since there is no MyClass object that it references, it cannot be used in a context where an actual MyClass object is needed:

import std.stdio;

class MyClass {
    int member;

void use(MyClass variable) {
    writeln(variable.member);    // ← BUG

void main() {
    MyClass variable;

As there is no object that is referenced by the parameter that use() receives, attempting to access a member of a non-existing object results in a program crash:

$ ./deneme
Segmentation fault

"Segmentation fault" is an indication that the program has been terminated by the operating system because of attempting to access an illegal memory address.

The null value

The special value null can be printed just like any other value.


The output:


A null variable can be used only in two contexts:

  1. Assigning an object to it
        variable = new MyClass;  // now references an object

    The assignment above makes variable provide access to the newly constructed object. From that point on, variable can be used for any valid operation of the MyClass type.

  2. Determining whether it is null

    However, because the == operator needs actual objects to compare, the expression below cannot be compiled:

        if (variable == null)     // ← compilation ERROR

    For that reason, whether a variable is null must be determined by the is operator.

The is operator

This operator answers the question "does have the null value?":

    if (variable is null) {
        // Does not reference any object

is can be used with other types of variables as well. In the following use, it compares the values of two integers:

    if (speed is newSpeed) {
        // Their values are equal

    } else {
        // Their values are different

When used with slices, it determines whether the two slices reference the same set of elements:

    if (slice is slice2) {
        // They provide access to the same elements
The !is operator

!is is the opposite of is. It produces true when the values are different:

    if (speed !is newSpeed) {
        // Their values are different
Assigning the null value

Assigning the null value to a variable of a reference type makes that variable stop referencing its current object.

If that assignment happens to be terminating the very last reference to the actual object, then the actual object becomes a candidate for finalization by the garbage collector. After all, not being referenced by any variable means that the object is not being used in the program at all.

Let's look at the example from an earlier chapter where two variables were referencing the same object:

    auto variable = new MyClass;
    auto variable2 = variable;

The following is a representation of the state of the memory after executing that code:

  (anonymous MyClass object)    variable    variable2
 ───┬───────────────────┬───  ───┬───┬───  ───┬───┬───
    │        ...        │        │ o │        │ o │
 ───┴───────────────────┴───  ───┴─│─┴───  ───┴─│─┴───
              ▲                    │            │
              │                    │            │

Assigning the null value to one of these variables breaks its relationship with the object:

    variable = null;

At this point there is only variable2 that references the MyClass object:

  (anonymous MyClass object)    variable    variable2
 ───┬───────────────────┬───  ───┬────┬───  ───┬───┬───
    │        ...        │        │null│        │ o │
 ───┴───────────────────┴───  ───┴────┴───  ───┴─│─┴───
              ▲                                  │
              │                                  │

Assigning null to the last reference would make the MyClass object unreachable:

    variable2 = null;

Such unreachable objects are finalized by the garbage collector at some time in the future. From the point of view of the program, the object does not exist:

                                variable      variable2
 ───┬───────────────────┬───  ───┬────┬───  ───┬────┬───
    │                   │        │null│        │null│
 ───┴───────────────────┴───  ───┴────┴───  ───┴────┴──

We had discussed ways of emptying an associative array in the exercises section of the Associative Arrays chapter. We now know a fourth method: Assigning null to an associative array variable will break the relationship of that variable with the elements:

    string[int] names;
    // ...
    names = null;     // Not providing access to any element

Similar to the MyClass examples, if names has been the last reference to the elements of the associative array, those elements would be finalized by the garbage collector.

Slices can be assigned null as well:

    int[] slice = otherSlice[ 10 .. 20 ];
    // ...
    slice = null;     // Not providing access to any element