-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Negative indices #827
Comments
The idea is good, though regarding ES nature has some subtle cases with inheritance -- since "-1" is just a simple string property. It was also discussed for the ECMAScript itself (http://bit.ly/bX8dzm, http://bit.ly/8ZRbCM). Python uses just negative indices (without *):
Also, in case of JS/Coffee, this thing should be generic, i.e. for any object which has Dmitry. |
Yup, that's the idea. |
Lua has a length operator, whereby D gives special treatment to
|
Excellent idea! Very common use case, and it's nice to see a precedent in another language. +1 |
This is nice for Coco -- but all the same arguments against it still apply from previous tickets. What we really want is true negative indices -- and that's something that has to be built in to the VM itself. |
What arguments from what previous tickets? I found two issues, from February and May, the latter being the more relevant (benlambert proposed at one point that You know that this will never be built into the VM itself, since The only cost is that people seeing |
Those previous tickets cover alternative syntaxes for negative indexing pretty thoroughly. That's about as exhaustive and focused as these conversations ever get -- and there was also healthy discussion on IRC. But what the hell, let's reopen it. My main beef with That said, my favorite of all the options proposed so far is benlambert's directed indexing operation, which means "count from the back of the array, (zero-based)":
|
|
@jashkenas
Meaningless -- yes, possibly. Although, what satyr showed regarding Perl 6, i.e.
is more than cryptic (though, maybe just for me, since I do not know Perl) and I don't think that such a complicated constructs are the Coffee's way. But, single
Yep. However, if to treat this symbol specially inside the P.S.: By the way, do you further plan to use JavaScript as an implementation level? I mean, not to implement it on some lower level language with VM, JIT, etc? If so, notice also, that ES6 (Harmony), as I mentioned above (http://bit.ly/8ZRbCM), possibly will have overloaded [[Get]] and [[Put]] semantics for array indicies with handling the negative ones. That means, that ECMAScript itself will have Python's semantics with just a Currently, this semantics can be already implemented in JS 1.8.1 (FF4) with supporting So if now Coffee chooses some other syntax for that semantics, it further may simply switch to just P.S.[2]: Syntactically, both Dmitry. |
@satyr flexible yes, but it looks like a cat walked over your keyboard :-P Until ES6, perhaps the simplest syntax is one Trevor suggested, just use |
That's how we do it in JS when feeling DRY: |
Re-closing this ticket ... We really don't want to have special syntax inserted into indexing operations. |
Just for fun:
|
Borrowing from Perl6, we could make it work by replacing the preceding
*
in the index operator with its operand's length. Something like:The text was updated successfully, but these errors were encountered: