'int' is enough for 'length' to migrate code from x86 to x64

H. S. Teoh via Digitalmars-d digitalmars-d at puremagic.com
Wed Nov 19 16:55:15 PST 2014


On Wed, Nov 19, 2014 at 04:42:53PM -0800, Andrei Alexandrescu via Digitalmars-d wrote:
> On 11/19/14 4:24 PM, H. S. Teoh via Digitalmars-d wrote:
> >On Wed, Nov 19, 2014 at 04:08:11PM -0800, Andrei Alexandrescu via Digitalmars-d wrote:
> >>On 11/19/14 12:40 PM, bearophile wrote:
> >>>Andrei Alexandrescu:
> >>>
> >>>>There are related bugs in Java too, e.g. I remember one in binary
> >>>>search where (i + j) / 2 was wrong because of an overflow.
> >>>
> >>>This is possible in D too.
> >>>
> >>>
> >>>>Also, Java does have a package for unsigned integers so apparently
> >>>>it's necessary.
> >>>
> >>>This is irrelevant. No one here is saying that a system language
> >>>should not have unsigned values. The discussion here is about the
> >>>type of array lengths.
> >>
> >>I think we're in good shape with unsigned. -- Andrei
> >
> >Implicit conversion between signed/unsigned is the fly that spoils
> >the soup, and the source of subtle bugs that persistently crop up
> >when dealing with size_t. The fact of the matter is that humans are
> >error-prone, even when they are aware of the pitfalls of mixing
> >signed / unsigned types, and currently the language is doing nothing
> >to help prevent these sorts of mistakes.
> 
> That I partially, fractionally even, agree with. We agonized for a
> long time about what to do to improve on the state of the art back in
> 2007 - literally months I recall. Part of the conclusion was that
> reverting to int for object lengths would be a net negative.
[...]

Actually, I agree about using unsigned for object lengths. I think it's
a sound decision -- why use a signed value for something that can never
be negative?

OTOH, what spoils this (hence the fly in soup reference), is the fact
that you can now take these unsigned values and randomly mix them up
with signed values without a single warning from the compiler. Even
requiring a cast to signify "I know what I'm doing, just do as I say"
would be an improvement over the current silent acceptance of
questionable code like `if (x.length - y.length < 0) ...`.


T

-- 
I am a consultant. My job is to make your job redundant. -- Mr Tom


More information about the Digitalmars-d mailing list