Why does intpromote spew warnings for ~ operator?

Adam D Ruppe destructionator at gmail.com
Tue Sep 14 13:01:29 UTC 2021


On Tuesday, 14 September 2021 at 11:53:48 UTC, Steven 
Schveighoffer wrote:
> In other words, if you have:
>
> ```d
> ushort x = 500;
> int y = -x;
> ```
>
> you would expect y to be -500. But in D it's 65036. Which makes 
> almost no sense at all.


Not if you actually understand what a ushort and the - operator 
does. And if you don't understand that, maybe you should not use 
ushort.

I'd note the same thing happens if you do an assign from uint to 
long.

         uint a = 500;
         long b = -a;
         writeln(b); // 4294966796

No deprecation warning either.


But yeah if you were copy/pasting from C I can see how it would 
be an unpleasant surprise. .... but then again speaking of long, 
if you copy/paste code from C that uses `long` to D, you are in 
for an unpleasant surprise since the size is different. sooooo 
meh.

> Another alternative would be to disable negation on unsigned 
> types. That would be also extremely disruptive.

The negation operator could also implicitly cast it to the signed 
version of the same size. Of course then you are liable to carry 
the one at the end but like at a certain point people need to 
understand what they're actually doing and just use plain int if 
they don't know.



but like if it would just stop complaining and making me double 
cast every operation i'd prolly be happy enough

the status quo is absurd


More information about the Digitalmars-d mailing list