Skip to content
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

Reverse indexing should be 0-based #3

Closed
jridgewell opened this issue Jan 21, 2021 · 4 comments
Closed

Reverse indexing should be 0-based #3

jridgewell opened this issue Jan 21, 2021 · 4 comments

Comments

@jridgewell
Copy link

I want make a parallel to regular forward indexing, and the easiest example is array iteration:

for (let i = 0; i < array.length; i++) {
  console.log(array[i]);
}

This form of = 0, < array.length, i++ is soooo common it's burned into my mind. Importantly, forward indexing is 0-based.

Now, compare that to the 1-based reversed indexing currently proposed:

for (let i = 1; i <= array.length; i++) {
  console.log(array[^i]);
}

That = 1 and <= is going to drive me crazy. It seems like if make reverse indexing 0-based instead, we can mirror C++'s std::rbegin (which is a 0-based iterator, it immediately points to the last item):

for (let i = 0; i < array.length; i++) {
  console.log(array[^i]);
}
@ljharb
Copy link

ljharb commented Jan 21, 2021

While i agree with the comparison, having ^0 be the -1st item will be very confusing.

@hax
Copy link
Owner

hax commented Jan 25, 2021

@jridgewell This question is well answered here: Why doesn't the new hat-operator index from the C# 8 array-slicing feature start at 0?

The main motivation of index from end syntax is not for iteration (which I believe reverse iterable would be a feature to satisfy such use cases in a better form), but for random accessing from end, and slice notation, in those cases, make ^0 represent "the end" (position after the last element) would be much convenient.

Note, of coz we could consider using C++ rbegin model, but:

  1. As current proposal, ^i could be think as a better -i (no -0 footgun), which make it very easy to get for the programmers have python, ruby experience (and JS already have slice/splice using -i). On the other side, as jordan said, "having ^0 be the -1st item will be very confusing" for them.
  2. Actually if follow rbegin, we can't use ^i syntax anymore because make same syntax do very same thing but with a significant semantic difference with another mainstream language like C# will cause very big confusion and discord. So we have to invent some other syntax, and it's really really hard.

@bmeck
Copy link

bmeck commented Feb 10, 2021

@jridgewell this would directly go against my being able to do arr[^0] = newTail;, having arr[^-1] would be wildly confusing

@hax
Copy link
Owner

hax commented Nov 16, 2023

As previous discussions, I think as the goal and constraint of this proposal and related proposal, it's very unlikely we could make a syntax using 0 to access the last (-1st) item. So I close this issue.

I add a FAQ item in README to link back to this issue, so if people have similar concerns they can continue the topic here, and we can reopen if needed.

@hax hax closed this as completed Nov 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants