Error on negating unsigned types

Johan Engelen via Digitalmars-d digitalmars-d at puremagic.com
Tue Jul 11 12:46:00 PDT 2017


The Weka folks would like to see a compile error on negating 
unsigned types:
```
uint total = 0;
void add(int x) { total += x; }
ubyte popCount() { return 5; }

add(popCount());
add(-popCount());
writeln(total); // <-- what does this print? (behavior is 
different from C)
```

After adding the error, a few places in Phobos error out.
For example:
``` (std.format.formatIntegral)
     T arg = val;

     immutable negative = (base == 10 && arg < 0);
     if (negative)
     {
         arg = -arg;
     }
```

``` (std.conv.toTextRange)
     T value;

     bool negative = value < 0;
     Unqual!(Unsigned!T) v = negative ? -value : value;
```

So, adding the error may be nice, but it would make generic code 
a little more verbose.
Ideas? People OK with that?

cheers,
   Johan



More information about the Digitalmars-d mailing list