On 1/18/19 12:26 PM, Mek101 wrote:
On Friday, 18 January 2019 at 17:15:09 UTC, Steven Schveighoffer wrote:
What is 127 + 127? Answer: 254. Which if converted to a byte is -127.
Not what you might expect if you are doing addition.
Quite similar to int.max + int.max
Indeed. But this is where C drew the line, so it's where D draws the
line as well.
In fact, D promotes all integral types smaller than int to int to do
arithmetic. And if the result might not fit into what you are
assigning it to, it requires a cast or mask.
Then why isn't int + int = long? If i did the example above, they
wouldn't fit in a int for sure, yet the result is of the same type. Why
should byte be any different?
There are other inconsistencies too, like += (which works on byte += byte).
As others have said, those are the rules D has for historical reasons,
you just have to deal with them.
-Steve