In the following code, I’d expect the output to be
B
C
But frustratingly enough it is
A
C
Is there something I can do to make it behave the way I was expecting? And why is this behavior happening in the first place?
I’ve resigned myself to defining mixin templates of stuff and mixing it in to every class that overrides write, which fixes the problem but is an ugly hack IMO.
import std.stdio : writeln;
class A {
void write() {
stuff();
}
void stuff()() {
writeln("A");
}
}
class B : A {
void stuff()() {
writeln("B");
}
}
class C : A {
void write() {
stuff();
}
void stuff()() {
writeln("C");
}
}
void main (string[] args) {
B b = new B();
b.write();
C c = new C();
c.write();
}
To quote the online documentation,