Negative indices for arrays

Brendan Eich brendan at mozilla.com
Fri Nov 12 14:35:09 PST 2010


I would have liked negative indexes for bracket-indexing from the end, and I suspect others who knew "in their bones" that it's too incompatible of a change would too -- we just knew it was not going to be a non-breaking change.

It's very hard to change subtle features that the Web comes to depend on. I've only seen building better mousetraps, leading people away from the old trap, work over time. It's not a speedy process, especially if the subtle feature is not a misfeature (but people disagree on this difference -- 'with' is still with us, some people actually defend and use it).

/be

On Nov 12, 2010, at 1:25 PM, Dmitry A. Soshnikov wrote:

> 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).
>> 
>> 
> 
> Sure.
> 
>>>> 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 -- `[].slice.call({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 http://hg.mozilla.org/tracemonkey 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/hg.mozilla.org/tracemonkey/OBJ/dist/MinefieldDebug.app/Contents/MacOS/components/nsSessionStore.js:1762:-1
>> http://i.i.com.com/cnwk.1d/html/rb/js/tron/oreo.moo.rb.combined.js:6:-1
>> http://i.zdnet.com/js/mootools-core-more.js:3:-1
>> http://l.yimg.com/a/combo?metro/tuc/tuc_outboxlite_0.0.29.js&metro/contentcarousel/contentcarousel_0.1.40.js&metro/contentcarousel/contentcarouseladserved_0.1.6.js&metro/uiplugins/toolbar_bridge_service_0.1.18.js&metro/uiplugins/tooltip_service_0.1.47.js&metro/uiplugins/sortable_service_0.1.20.js&metro/pa/pautil_0.1.4.js&metro/pa/palist_0.1.23.js&metro/pa/pa_0.1.218.js&metro2/multimedia/multimedia_0.0.6.js&metro/batchload/batchload_0.1.7.js:7:-1
>> 
>> 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.
> 
> Dmitry.
> 
>> /be
>> 
> 



More information about the es-discuss mailing list