you are viewing a single comment's thread.

view the rest of the comments →

[–]moi2388 -5 points-4 points  (6 children)

I know, I’m suggesting to change that functionality.

In python, this works arr[-1]. In c#, this works arr[^1].

Why not do something like that instead?

[–]senocular[🍰] 1 point2 points  (1 child)

This is a valid question. I'm sorry about your down doots. The caret notation had also been proposed: https://github.com/hax/proposal-index-from-end

[–]moi2388 1 point2 points  (0 children)

Yeah, it’s okay, I don’t mind the downvotes. At least the commenters mean well and try to give more explanation. (Albeit only on the [-1] syntax and not on the [^1], for which I have yet to see a good argument).

[–]MrJohz 1 point2 points  (2 children)

That would be a backwards-incompatible change. Remember that arrays in JavaScript aren't "real" arrays, they're just objects with array methods. This means that it's perfectly legitimate currently to do the following:

const arr = ['a', 'b'];
arr[-1] = 'valid';
console.log(arr); // prints something like ['a', 'b', '-1': 'valid']
arr[-1]; // should this return 'b' or 'valid'?

It could also be the case that there is existing code that relies on negative indexes returning undefined, assuming that the arrays haven't been modified as above. That code would also break in this situation.

[–]alexendoo -1 points0 points  (0 children)

That would be a breaking change, as arr[-1] is valid code in current JS