Operator Overloading - Only for Classes and Structs?
Andy Valencia
dont at spam.me
Tue Jun 24 03:56:15 UTC 2025
I was a little surprised the subclassing syntax didn't do
structural concatenation for struct's. That is,
```d
import std.stdio : writeln;
struct A {
int a;
void printA() {
println(this.a);
}
}
struct B : A {
int b;
void printAB() {
this.printA();
println(this.b);
}
}
void an_A_fun(A arg) {
arg.printA();
}
void main() {
B foo;
foo.a = 1;
foo.b = 2;
foo.printAB();
an_A_fun(foo);
}
```
Unlike classes, this would guarantee storage adjacency, and still
preserve its behavior as a value rather than an object reference.
And then you get to share code for common initial parts of a
given struct.
More information about the Digitalmars-d-learn
mailing list