Negative indices for arrays

Dmitry A. Soshnikov dmitry.soshnikov at
Fri Nov 12 01:01:40 PST 2010

On 12.11.2010 2:40, Brendan Eich wrote:
> I agree with Dave, Allen, and Oliver

That's OK.

I consider several variants also. As Allen mentioned, if it will be a 
generic thing, then `foo[-n]` may break an old code (and then we need 
another syntax e.g. a[* - 1] -- if need at all then!). OTOH, if only for 
arrays -- I don't see any big _real practical_ issues.

> that we should not just change indexing under Harmony script-type opt-in.
> Note also that Python, at least, has a more elaborate system of slicing that has evolved over the years. I added slice in the Netscape 4 era, which made it into ES3 and has the desired negative index behavior, but other additions from that era (some from Perl!) do not match, and anyway, the array-index-is-a-property-name ship had sailed in 1995.

Of course it had sailed. But the thing is nobody (?) on that ship used 
negative-array-index in old code -- in a some more-less serious _real 
practical_ application. So it may be re-borrowed and put on the next 
ship. However, not necessary in this view -- a[-1]. If generic, -- with 
another syntax.

> If we were to develop a generic slice proposal with new syntax, then I think it would be crazy not to follow Python's (and ES3-5's slice method's) lead and support negative indexing. This would not break existing code because it would happen only with new syntax and new functions or methods.


> The obvious slice syntax is a[i:j] for a slice of a from index i up to but not including index j, with negative indexes supported as from the end, and with indexes on either side of : optional, defaulting as in Python.
> If : is problematic we could try CoffeeScript's .., but .. and ... as well as . in the language may be too much. The syntax is not as important as the semantics at this point, but one more syntax observation:
> The [:] syntax fits in Python, where a[-1] is the last element of the sequence a.

> But since we don't want to make that change to JS arrays,

So, can we stop at this point? I mean -- if we really don't want this 
changes, then the question may be closed, I guess. Just acknowledge it.

>   this slice syntax is not so attractive. It will mislead Python folks into using negative indexes with property accesses as well as with slices.

Yeah, true. Though, maybe nevertheless to completely port Python's 
slicing for arrays? ;P I liked this idea. But -- only for arrays, not 
generic then. Unfortunatelly, it may break current generic slice -- 
`[]{0:1, 1:2, length:2}, -1)[0]` // 2.

> Ruby does not dedicate syntax, using methods instead (Smalltalk-y, as one would hope).

How that? Coffee borrows its .. (including higher index) and ... 
(excluding higher index) from Ruby:


a = [1, 2, 3, 4]
a[-1] # 4

a[0..2] # [1,2,3]
a[0...2] # [1,2]

(need to say, that for me .. and ... should be logically inverted: i.e. 
... should include, and .. -- exclude).

> This seems much more promising for Harmony than new slice or from-the-end indexing syntax, since we already have Array and String slice methods that do the right thing with negative indexes.

Yes, regarding slice -- yes. And moreover, they are generic.

>   We have String charAt/charCodeAt but part of the appeal of slicing and negative indexing is how they work on all sequence types in Python.
> An Array and String get method that takes an index, which can be negative, would complement slice. Array would want a set or put method too. Names need tuning, but they ought to be short.


> /be
> On Nov 11, 2010, at 3:25 PM, David Herman wrote:
>>> If harmony would introduce this syntax "guarded" under a new script type, there
>>> would at least be no danger of breaking the web (existing scripts).
>> That sounds like an interop nightmare -- you're talking about forking the Array type between language versions. Keep in mind that non-Harmony and Harmony code will be able to interact in the same page.
>> Dave
>> _______________________________________________
>> es-discuss mailing list
>> es-discuss at
> _______________________________________________
> es-discuss mailing list
> es-discuss at

More information about the es-discuss mailing list