Negative indices for arrays
Dmitry A. Soshnikov
dmitry.soshnikov at gmail.com
Fri Nov 12 00:36:55 PST 2010
On 12.11.2010 1:17, Allen Wirfs-Brock wrote:
>> -----Original Message-----
>> From: Dmitry A. Soshnikov [mailto:dmitry.soshnikov at gmail.com]
>> Yeah, it's possible to make this thing generic, though maybe also good only for
>> arrays. Need to more discuss, think.
> There isn't actually all that much difference between array instances and non-array objects with array index properties. ES5 went in the direction of making sure that array functions all worked on arbitrary objects. I don't think we would want to go backwards on that.
Yeah, however, I think it's mostly not about generic methods, but about
overloaded [[Get]] / [[Put]]. And in ES3 and ES5 overloaded [[Put]] /
[[DefineOwnProperty]] is not generic but special for arrays. The same I
assume this feature is special for arrays. Really, I don't see any big
issues with backward compats. They are so minimal that may be ignored.
>>> this would break any code that currently uses property names like "-1".
>> I repeat, we tried to find and remind at least _one_ more-less serious use-case
>> where negaitve indices where used in the old code with array -- and can't. Do
>> you know any? It will be useful if you show.
> Anybody who is using an object (possibly an array instance) as a hash table with signed integer keys
That's another case. Of course, if we'll consider the other objects,
then "-1" may be used. But, repeat, this feature (at least with the
syntax `a[-n]`) should be considered first/only for arrays.
Another thing I agree we may want to see in JS a generic such a feature,
then the alternative syntax as `a[* - 1]` may be the case. That, repeat,
will bring also a sugar for `a.push(10)` as `a[*] = 10`.
>>> This isn't just syntactic sugar. It also has all sorts of complications relating to
>> the basic property access semantics. Getting this right would probably require a
>> major redesign of ECMAScript object/property semantics in a manner that I
>> wouldn't anticipate happening anytime soon.
>> Thanks. Though, I don't see a bit changes (how you like to described all in dark
>> colors :)). At maximum (if not generic) -- only [[Get]] of arrays and strings will be
> And presumably [[Put]] and as some sort of dynamic parsing of string property names and for generic object support check for a length property, etc.
>> So, do I understand correctly that you are against this feature and don't like it?
>> (Just another question -- are you aware that it used in Python, Ruby, Perl,
>> Coffee, other langs?)
But what are suggesting at the same time? What's your meaning -- whether
JS needs a some sugar for taking elements (ok, let's say) of a sequence
starting-from-the-end or not?
More information about the es-discuss