On Sunday, 22 February 2015 at 07:11:24 UTC, deadalnix wrote:
On Sunday, 22 February 2015 at 02:27:30 UTC, 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.
The rationale make sens for things like :
byte a; a += 1;
Here, because of type promotion, a + 1 is an int, and if VRP of
a is unknown, you can't cast implicitly back to byte.
If VRP is unknown then it should be disallowed for precisely that
reason! It can't implicitly cast back to a byte, so it shouldn't.
If you know that the int fits in a byte then do the cast
yourself, that's what it's for.
It is true that this create questionable side effect for float,
but in the end, that is consistent, and it would be very
annoying to not be able to increment/decrement integral smaller
than int.
Even if you forget about float, it's still inconsistent.
byte a;
int b;
a = a + b;
a += b;
These do the same thing, and have the same pitfalls if the int is
too big. Both should be disallowed.