Treating the abusive unsigned syndrome
Andrei Alexandrescu
SeeWebsiteForEmail at erdani.org
Wed Nov 26 07:24:17 PST 2008
Michel Fortin wrote:
> 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.
Well that at best takes care of _some_ operations involving constants,
but for example does not quite take care of array.length - 1.
I am now sorry I gave the silly example of array.length + 1. Many people
latched on it and thought that solving that solves the whole problem.
That's not quite the case.
Also consider:
auto delta = a1.length - a2.length;
What should the type of delta be? Well, it depends. In my scheme that
wouldn't even compile, which I think is a good thing; you must decide
whether prior information makes it an unsigned or a signed integral.
> 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.
That can be formalized by having polysemous types have a "lemma", a
default type.
> 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.
Fine. With constants there is some mileage that can be squeezed. But
let's keep in mind that that doesn't solve the larger issue.
Andrei
More information about the Digitalmars-d
mailing list