D Language Structs Struct Constructors


Example

In D we can use constructors to initialize structs just like a class. To define a construct for the struct declared in the previous example we can type:

struct Person {
    this(int age, int height) {
        this.age = age;
        this.height = height;
        this.ageXHeight = cast(float)age * height;
    }
}

auto person = Person(18, 180);