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

David Gileadi via Digitalmars-d digitalmars-d at puremagic.com
Wed Nov 19 08:22:31 PST 2014


On 11/19/14, 9:12 AM, bearophile wrote:
> David Gileadi:
>
>>     writefln("%s", b.length - a.length);  // Yup, 2
>>     writefln("%s", a.length - b.length);  // WAT? 18446744073709551614
>
> Nowadays a better way to write such kind of code is using the Phobos
> "signed" function:
>
> writefln("%s", b.length.signed - a.length.signed);
> writefln("%s", a.length.signed - b.length.signed);

But this requires the following knowledge:
1. That the signed function exists, and its location. As a casual D 
programmer I didn't know about it.
2. That there's a need for it at all, which requires knowing that length 
is unsigned. I did know this, but I bet in the heat of programming I'd 
easily forget it. In a semi-complex algorithm the bug could easily hide 
for a long time before biting.

>> This is why I agree with Don that:
>>
>> > Having arr.length return an unsigned type, is a dreadful
>> language mistake.
>
> This mistake is by design. Walter has resisted several times turning
> them into signed values, and even making size_t a type strongly
> different from uint/ulong. I don't agree with this design decision, but
> it's unlikely that Walter has changed his mind on it. So better to go on
> and discuss other things more likely to happen.

Yes, I bet you're right about the likelihood of change.


More information about the Digitalmars-d mailing list