Why can't we derive struct's?

Rubn where at is.this
Tue Dec 25 12:50:50 UTC 2018

On Tuesday, 25 December 2018 at 11:31:17 UTC, Walter Bright wrote:
> On 12/24/2018 6:43 PM, Rubn wrote:
>> You can easily make the "+" operator do something entirely 
>> different than addition.
> If you want to make "+" non-commutative, you have to work a 
> little harder.

Not that hard at all, you just do operator overloading like 


struct Int {
     int value;
     this( int input ) {
         value = input;

     Int opBinary(string op)(int a) const if ( op == "+" ) {
         return Int(value + a);
     // Hell can make this return a different type and you
     // won't be even be able to do the comparison: (a + b) == (b 
+ a)
     Int opBinaryRight(string op)(int a) const if ( op == "+" ) {
         return Int(value + a + 1);

void main() {
     import std.stdio : writeln;
     const Int a = 5;
     const int b = 2;

     writeln(a + b, " == ", b + a);

> I'm happy to see that D has successfully inculcated a culture 
> that attempting to use operator overloading for non-arithmetic 
> purposes is a code smell, rather than celebrating it as C++ 
> does. It's like:
> #define BEGIN {
> #define END }
> is considered a smell in C (it didn't use to be, it took a 
> while to stamp that out).

What makes you think C++ celebrates it? You just point to one 
person that thinks it's a good idea. The C++ community has tens 
of thousands more users than D, there are going to be people that 
think it is a good idea.

> We'll not be accepting such practices into the D standard 
> library, etc., and will do what we can to discourage such.
> So yes, if you try hard enough, you can write execrable code. 
> But you'll be on your own with it.

Sadly auto-decoding has already made it into the standard. I'm 
happy to say auto-decoding won't be making its way into the C++ 
standard :).

More information about the Digitalmars-d mailing list