Array start index

John Colvin via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Aug 1 10:55:04 PDT 2015


On Saturday, 1 August 2015 at 09:35:53 UTC, DLearner wrote:
> Does the D language set in stone that the first element of an 
> array _has_ to be index zero?

For the builtin slice types? Yes, set in stone.

> Wouldn't starting array elements at one avoid the common 
> 'off-by-one' logic error, it does
> seem more natural to begin a count at 1.
>
> Actually, maybe even better to allow array definitions of form
> int foo[x:y];
> (y >= x) creating integer variables foo[x], foo[x+1],...,foo[y].
>
> I think the (very old) IBM PL/I language was like this.

See 
https://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/EWD831.html

As other commenters have/will point out, you can easily define a 
custom type in D that behaves as you describe, and - Dijkstra 
notwithstanding - there are valid uses for such things.


More information about the Digitalmars-d-learn mailing list