Please tell me this is a bug?

Steven Schveighoffer via Digitalmars-d digitalmars-d at puremagic.com
Mon Feb 23 11:07:24 PST 2015


On 2/21/15 9:27 PM, 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.

I think the docs are misleading.

For example, this doesn't work:

int a;
int *b = &a;

a += b;

Error: incompatible types for ((a) += (b)): 'int' and 'int*'

But this does:

a = cast(typeof(a))(a + b);

I think it only works if it's a numeric narrowing conversion, but I 
don't know for sure. I would be happy if this behavior changed as the OP 
expected. But I'm glad there isn't exactly an unfettered implicit cast 
in every op= operation.

Does anyone know the true rules for this, and can they please file an PR 
on the docs?

-Steve


More information about the Digitalmars-d mailing list