Should negating an unsigned integral be an error?

Michael michael at toohuman.io
Tue Jan 30 11:09:08 UTC 2018


On Monday, 29 January 2018 at 10:06:41 UTC, Jonathan M Davis 
wrote:
> On Monday, January 29, 2018 09:58:00 Dave Jones via 
> Digitalmars-d wrote:
>> Given
>>
>> uint i = 12345;
>>
>> should
>>
>> writeln(-i)
>>
>> be an error? or maybe i should be automatically cast to a 
>> larger signed type?
>
> It arguably should be, but it's pretty common in C/C++ to get 
> uint.max with -1, and I suspect that it's fairly common in D as 
> well even though you could just use uint.max. So, it could be 
> argued either way.
>
> - Jonathan M Davis

I'd like to see these kinds of potential mistakes to be forced to 
use something like uint.max. Of course, at this point it might be 
too late to introduce something as breaking as this, but these 
kinds of things should definitely be forced by the language, or 
made a warning at least, pointing to the use of .max instead.


More information about the Digitalmars-d mailing list