Programming in D – Tutorial and Reference
Ali Çehreli

Other D Resources

alias this

We have seen the individual meanings of the alias and the this keywords in previous chapters. These two keywords have a completely different meaning when used together as alias this.

alias this enables automatic type conversions (also known as implicit type conversions) of user-defined types. As we have seen in the Operator Overloading chapter, another way of providing type conversions for a type is by defining opCast for that type. The difference is that, while opCast is for explicit type conversions, alias this is for automatic type conversions.

The keywords alias and this are written separately where the name of a member variable or a member function is specified between them:

    alias member_variable_or_member_function this;

alias this enables the specific conversion from the user-defined type to the type of that member. The value of the member becomes the resulting value of the conversion .

The following Fraction example uses alias this with a member function. The TeachingAssistant example that is further below will use it with member variables.

Since the return type of value() below is double, the following alias this enables automatic conversion of Fraction objects to double values:

import std.stdio;

struct Fraction {
    long numerator;
    long denominator;

    double value() const @property {
        return double(numerator) / denominator;
    }

    alias value this;

    // ...
}

double calculate(double lhs, double rhs) {
    return 2 * lhs + rhs;
}

void main() {
    auto fraction = Fraction(1, 4);    // meaning 1/4
    writeln(calculate(fraction, 0.75));
}

value() gets called automatically to produce a double value when Fraction objects appear in places where a double value is expected. That is why the variable fraction can be passed to calculate() as an argument. value() returns 0.25 as the value of 1/4 and the program prints the result of 2 * 0.25 + 0.75:

1.25
Multiple inheritance

We have seen in the Inheritance chapter that classes can inherit from only one class. (On the other hand, there is no limit in the number of interfaces to inherit from.) Some other object oriented languages allow inheriting from multiple classes. This is called multiple inheritance.

alias this enables using D classes in designs that could benefit from multiple inheritance. Multiple alias this declarations enable types to be used in places of multiple different types.

Note: dmd 2.074.0, the compiler that was used last to compile the examples in this chapter, allowed only one alias this declaration.

The following TeachingAssistant class has two member variables of types Student and Teacher. The alias this declarations would allow objects of this type to be used in places of both Student and Teacher:

import std.stdio;

class Student {
    string name;
    uint[] grades;

    this(string name) {
        this.name = name;
    }
}

class Teacher {
    string name;
    string subject;

    this(string name, string subject) {
        this.name = name;
        this.subject = subject;
    }
}

class TeachingAssistant {
    Student studentIdentity;
    Teacher teacherIdentity;

    this(string name, string subject) {
        this.studentIdentity = new Student(name);
        this.teacherIdentity = new Teacher(name, subject);
    }

    /* The following two 'alias this' declarations will enable
     * this type to be used both as a Student and as a Teacher.
     *
     * Note: dmd 2.074.0 did not support multiple 'alias this'
     *       declarations. */
    alias teacherIdentity this;
    alias studentIdentity this;
}

void attendClass(Teacher teacher, Student[] students)
in {
    assert(teacher !is null);
    assert(students.length > 0);

} body {
    writef("%s is teaching %s to the following students:",
           teacher.name, teacher.subject);

    foreach (student; students) {
        writef(" %s", student.name);
    }

    writeln();
}

void main() {
    auto students = [ new Student("Shelly"),
                      new Student("Stan") ];

    /* An object that can be used both as a Teacher and a
     * Student: */
    auto tim = new TeachingAssistant("Tim", "math");

    // 'tim' is the teacher in the following use:
    attendClass(tim, students);

    // 'tim' is one of the students in the following use:
    auto amy = new Teacher("Amy", "physics");
    attendClass(amy, students ~ tim);
}

The output of the program shows that the same object has been used as two different types:

Tim is teaching math to the following students: Shelly Stan
Amy is teaching physics to the following students: Shelly Stan Tim