Treating the abusive unsigned syndrome

Michel Fortin michel.fortin at michelf.com
Wed Nov 26 07:16:50 PST 2008


On 2008-11-25 16:39:05 -0500, Andrei Alexandrescu 
<SeeWebsiteForEmail at erdani.org> said:

> Russell Lewis wrote:
>> I'm of the opinion that we should make mixed-sign operations a 
>> compile-time error.  I know that it would be annoying in some 
>> situations, but IMHO it gives you clearer, more reliable code.
> 
> The problem is, it's much more annoying than one might imagine. Even 
> array.length - 1 is up for scrutiny. Technically, even array.length + 1 
> is a problem because 1 is really a signed int. We could provide 
> exceptions for constants, but exceptions are generally not solving the 
> core issue.

Then the problem is that integer literals are of a specific type. Just 
make them polysemous and the problem is solved.

I'm with Russel on this one. To me, a litteral value (123, -8, 0) is 
not an int, not even a constant: it's just a number which doesn't imply 
any type at all until you place it into a variable (or a constant, or 
an enum, etc.).

And if you're afraid the word polysemous will scare people, don't say 
the word and call it a "integer litteral". Polysemy in this case is 
just a mechanism used by the compiler to make the value work as 
expected with all integral types. All you really need is a type 
implicitly castable to everything capable of holding the numerical 
value (much like your __intuint).

I'd make "auto x = 1" create a signed integer variable for the sake of 
simplicity.

And all this would also make "uint x = -1" illegal... but then you can 
easily use "uint x = uint.max" if you want to enable all the bits. It's 
easier as in C: you don't have to include the right header and remember 
the name of a constant.

-- 
Michel Fortin
michel.fortin at michelf.com
http://michelf.com/




More information about the Digitalmars-d mailing list