Why is int implicitly convertible to ulong?

Ali Çehreli acehreli at yahoo.com
Sun Feb 16 14:27:29 PST 2014


On 02/16/2014 01:35 PM, Hannes Steffenhagen wrote:
> isImplicitlyConvertible!(int,ulong) is true. Maybe this is just me, but
> I get the impression that this is quite nuts. Why is an implicit
> conversion from a signed to an unsigned type possible? The other way
> round would be at least somewhat understandable if there's a static
> check that the values actually fit.

I don't know all of the reasons but it is at least about convenience. It 
is possible to write expressions like 'u + diff' without explicit casts:

     ulong u = 10;
     int diff = -3;
     auto a = u + diff;
     static assert(is (typeof(a) == ulong));

Related, the type of 'a' above is implied as ulong due to arithmetic 
conversions, which are sometimes very confusing as well. See "Usual 
Arithmetic Conversions" here:

   http://dlang.org/type.html

Ali



More information about the Digitalmars-d mailing list