complement to $

bearophile bearophileHUGS at lycos.com
Sat May 15 15:03:46 PDT 2010


Walter Bright:

> Sure you can:
>      last_index = a.length - 1;

Probably my post was nearly useless, because it doesn't help much the development of D, so you can ignore most of it.
The only part of it that can be meaningful is that it seems Python designers have though that having a way to specify the _generic_ idea of start of a slice can be useful (with a syntax like a[:end] or a[None:end]) so in theory an equivalent syntax (like a[^..end]) can be added to D, but I have no idea if this is so commonly useful in D programs. If you notice in this thread I have not said that I like or dislike the 'complement to $' feature.

Regarding your specific answer here, storing a.length-1 in the index is not able to represent the idea of "last item". Another example to show you better what I meant:

last_index = -1
a = ['a', 'b', 'c']
b = [1.5, 2.5, 3.5, 4.5]
assert a[last_index] == 'c'
assert b[last_index] == '4.5'

I am not asking for this in D, I don't think there are simple ways to add this, and I don't need this often in Python too. I am just saying that the semantics of negative indexes in Python is a superset of the D one.

Bye,
bearophile


More information about the Digitalmars-d mailing list