Please tell me this is a bug?

Daniel Kozak via Digitalmars-d digitalmars-d at puremagic.com
Sun Feb 22 12:08:34 PST 2015


ketmar via Digitalmars-d píše v Ne 22. 02. 2015 v 03:15 +0000:
> On Sun, 22 Feb 2015 02:27:29 +0000, Peter Alexander wrote:
> 
> > On Sunday, 22 February 2015 at 01:24:09 UTC, Almighty Bob wrote:
> >> a += b; // Compiles with no ERROR!
> >>
> >> Please tell me that's a bug?
> > 
> > Not a bug. From spec:
> > 
> > http://dlang.org/expression.html#AssignExpression
> >> Assignment operator expressions, such as:
> >> 
> >> a op= b
> >> 
> >> are semantically equivalent to:
> >> 
> >> a = cast(typeof(a))(a op b)
> > 
> > Seems questionable to me. Anyone know the rationale? If a = b; is
> > disallowed, I don't see why a += b; should be more acceptable.
> 
> it's fuuuuuunny!
> 
>   struct A {
>     int v = 40;
>     this (int n) { v = n; }
>     int opOpAssign(string op) (A b) { return v+b.v; }
>   }
> 
>   void main () {
>     auto a = A();
>     a += (cast(A)5);
>     // import std.stdio; writeln(a.v); // can you guess the output?
>   }
> 
> 'cmon, guess it without executing the code! but don't ask me why this 
> psychodelic code can be compiled at all.
nothing has change because you dont assign anything to v so I guess you
mean this code?:

import std.stdio;

struct A {
     int v = 40;
     this (int n) { v = n; }
     void opOpAssign(string op) (A b) { v+=b.v; }
}

void main () {
     auto a = A();
     a += (cast(A)5);
     writeln(a.v); // can you guess the output?
}

than it would be obviosly 45



More information about the Digitalmars-d mailing list