Why D is not popular enough?

Walter Bright via Digitalmars-d digitalmars-d at puremagic.com
Sun Aug 14 11:45:06 PDT 2016


On 8/14/2016 11:03 AM, Shachar Shemesh wrote:
>> Which rule?
> The rule that says "ubyte + ubyte = uint".

Many people are surprised by this rule, but it is deeply embedded in C and C++.

In C99,

-------------------------------
6.3.1.8 Usual Arithmetic Conversions

Then the following rules are applied to the promoted operands:

If both operands have the same type, then no further conversion is needed.

Otherwise, if both operands have signed integer types or both have unsigned
integer types, the operand with the type of lesser integer conversion rank is
converted to the type of the operand with greater rank.

Otherwise, if the operand that has unsigned integer type has rank greater or
equal to the rank of the type of the other operand, then the operand with
signed integer type is converted to the type of the operand with unsigned
integer type.

Otherwise, if the type of the operand with signed integer type can represent
all of the values of the type of the operand with unsigned integer type, then
the operand with unsigned integer type is converted to the type of the
operand with signed integer type.

Otherwise, both operands are converted to the unsigned integer type
corresponding to the type of the operand with signed integer type.
--------------------------------------------

This rule was retained for D to make it easier to translate code from C/C++ to 
D. Changing the rule could result in subtle and invisible bugs for such 
translations and for C/C++ programmers who are so used to the integral promotion 
rules that they aren't even really aware of reliance upon them.

The reason C/C++ programmers, even experienced ones, are often unaware of this 
rule is there is another rule, implicit narrowing, so:

     byte = byte + byte;

compiles without complaint. The trouble comes when byte has a value, say, 255, 
and the sum is 510. The assignment silently chops it to byte size, and 254 is 
stored in the result.

For D, we decided that silently converting 510 to 254 would not be acceptable. 
Hence an explicit cast would be required,

     byte = cast(byte)(byte + byte);

where the user presumably knows what he's doing.


More information about the Digitalmars-d mailing list