Negative indices for arrays

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

Brendan, thanks for the analysis and tests, possibly it's useful. Also, 
I think that a proposal can look more like a proposal (and not like I'm 
very want to include something into the language, and other members 
resist it), I'm not sure we need to prove something in this case. If 
everybody on the lists have decided that this is not needed for 
ECMAScript -- that's completely OK.

(Below some small additions)

On 12.11.2010 18:40, Brendan Eich wrote:
> On Nov 12, 2010, at 1:01 AM, Dmitry A. Soshnikov wrote:
>> 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.
> It is dangerous to make assertions based on wishes, instead of gathering data (see below).


>>> 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.
> You do keep asserting that. :-|

Of course. Though, we may say that neither Allen (including Erick from 
V8), nor Oliver didn't even asserted. Just some abstract "dramatic", 
"critical" breaking of the old code. But in real, I didn't see it. Your 
tests with logging maybe prove something, but hardly it's an absolute 
and real results. Anyway, it's something.

>> 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?
> I was referring to slice.

Hm... These are slices:

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

a[-1..-1] # 4
a[-1] # the same, 4

Btw, in ES, slice being able to slice:

a.slice(-2, -1); // [3]

unable to slice:

a.slice(-2, -1); // []

and requires:

a.slice(-1)[0]; // 4

but this is -- JFTR.

> Ok, data: I added logging code to detect whenever an array is accessed via a negative index to my custom Firefox build. Here after a few minutes of browsing is the  log file, piped through sort -u (there are many repeated entries):
> file:///Users/brendaneich/Hacking/
> I'll keep this browser running for a bit, but it is clear "somebody" used negative array indexes. And it would be no fun finding such dynamic occurrences when porting if we made negative indexes work as in Python in Harmony.

Maybe they tried to apply exactly Python's semantics while were testing? 
:D But seriously, yep, the test may say something.

> In general, taking ES5 strict code into Harmony should not require porting, other than removing any dependencies on global variable vs global object property aliasing.

OK, so since all are against, I think the proposal (if is not required 
hard) may be canceled.

Thanks again.


> /be

More information about the es-discuss mailing list