'int' is enough for 'length' to migrate code from x86 to x64
Walter Bright via Digitalmars-d
digitalmars-d at puremagic.com
Thu Nov 20 00:14:28 PST 2014
On 11/19/2014 10:09 AM, Ary Borenszweig wrote:
> I agree. An array's length makes sense as an unsigned ("an array can't have a
> negative length, right?") but it leads to the bugs you say. For example:
>
> ~~~
> import std.stdio;
>
> void main() {
> auto a = [1, 2, 3];
> auto b = [1, 2, 3, 4];
> if (a.length - b.length > 0) {
> writeln("Can you spot the bug that easily?");
Yes.
> }
> }
> ~~~
>
> Yes, it makes sense, but at the same time it leads to super unintuitive math
> operations being involved.
Computer math is not math math. It is its own beast, and if you're going to
write in a systems programming language it is very important to learn how it
works, or you'll be nothing but frustrated.
> Rust made the same mistake and now a couple of times I've seen bugs like these
> being reported. Never seen them in Java or .Net though. I wonder why...
D is meant to be easily used by C and C++ programmers. It follows the same model
of signed/unsigned arithmetic and integral promotions. This is very, very
deliberate. To change this would be a disaster.
For example, in America we drive on the right. In Australia, they drive on the
left. When I visit Australia, I know this, but when stepping out into the road I
instinctively check my left for cars, step into the road, and my foot gets run
over by a car coming from the right. I've had to be very careful as a pedestrian
there, as my intuition would get me killed.
Don't mess with systems programmers' intuitions. It'll cause more problems than
it solves.
More information about the Digitalmars-d
mailing list