Please tell me this is a bug?

ketmar via Digitalmars-d digitalmars-d at puremagic.com
Sat Feb 21 19:15:25 PST 2015


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.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 181 bytes
Desc: not available
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20150222/1b0ca67e/attachment.sig>


More information about the Digitalmars-d mailing list