Array indexing and slicing
Luís Marques
luismarques+spam at gmail.com
Mon Apr 3 10:37:14 PDT 2006
Hello,
In the array section of the D docs (http://www.digitalmars.com/d/arrays.html),
there is no mention of the $ operator. Only the "a = a[2 .. a.length]" idiom is
shown. I suppose that should be fixed.
My experience of array slicing comes from Python, so I found it a bit surprising
that a "array[2..]" notation didn't exist ("array[2:]" in Python). I later found
the $ operator in others' code, which mostly fixes that.
How about supporting, as in Python:
a[-1] == a[$];
a[0..] == a[0..$];
I personally dislike the $ operator, it seems a casual fix for a lack of better
notation and someting you'd expect to find in Perl. I still prefer it to the
more verbose "a[2 .. a.length]" notation, though. The verbose notation has the
disadvantage of taking more whitespace; while most people read "a[2..3]" just
fine it's a bit confusing to read "a[1..a.length]". Also, if you rename or use a
different array you have two instances to correct, instead of one.
The Python syntax seems to me a better generalization of the concepts. It
doesn't require an extra operator, the indexes and the slice notation have all
the expressiveness required.
With omissive indexes (a[..7]) and wrapping indexes (a[-2]) the $ could go away.
Is it too late for that?
Luís Marques
More information about the Digitalmars-d
mailing list