Question

Is it possible to inherit constructors in D?

abstract class A {
    this(int a) {
        // ...
    }
}

class B: A {}

void main() {
    B b = new B(2); // Use A's constructor
}

I know I could just call A's constructor in B by doing super(param), but there'll be a lot of classes going on so I was wondering if there is a more automated way of doing it.

Was it helpful?

Solution

I don't think there's a language feature for it, but you can implement this as a helper mixin:

https://github.com/CyberShadow/ae/blob/29aa5dde35677a38dc8f6315dc38d7ecd437d967/utils/meta/misc.d#L219-L267

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top