tc39 / proposal-deiter

Double-Ended Iterator and Destructuring
MIT License
70 stars 3 forks source link

Use a word clearer than "back"? #9

Closed hax closed 2 years ago

hax commented 2 years ago

It seems the api next("back") easily cause confusion between "double-ended" with "bidirectional".

I adopted the word "back" from Rust double-ended Iterator nextBack() method, and I believe they adopted "back" from C++ vector/deque::back. Maybe we should use a much clearer phrase?

Options come into my mind:

In all these options, I prefer next("last"). It matches the meaning of "last" in current APIs "lastIndexOf", "findLast". Actually C++ vector/deque just use "front/back" to denote "the first/last element", so we'd better restore the word to "last".

ljharb commented 2 years ago

I think passing a magic string into next to that it means "something other than next" is always going to be confusing.

hax commented 2 years ago

"something other than next"

Iterator.prototype.next() already could accept an extra param, so next(x) means "give me the next value according to x. In next("last") case, it means "give the next value from last", just like findLast of tc39/proposal-array-find-from-last means "find the value from last". Even we use a separate method nextBack() or nextLast(), it eventually need to invoke next() so user-land generators could receive it.

@ljharb I agree string seems magical, but JS APIs always use strings for enum (hope we can have enum proposal advanced in the future). Would symbol (Symbol.last) better than string? (Though I think symbols are for keys.)

ljharb commented 2 years ago

It's the "next" method name that makes it confusing, not just the magic string.

ljharb commented 2 years ago

imo the word "next" always means "the one after" in english; the next thing can never be the previous thing.

hax commented 2 years ago

It's the "next" method name that makes it confusing, not just the magic string.

I'm not sure how "iterator.next" could be renamed to reduce the confusion, even there was option, we can't "fix" it now.

imo the word "next" always means "the one after" in english; the next thing can never be the previous thing.

Well, next("last") does not mean "previous thing", this is why I want to rename it from "back" to "last" or something else, because next("back") make it look like "previous thing".

ljharb commented 2 years ago

I just don't think there's any way it would make sense for "next" - regardless of argument - to return something that wasn't actually the next item, or one of the remaining items.

tmaxmax commented 2 years ago

While the name next in itself is confusing, we should still strive to add clarity by any margin possible. Having the magic string as 'last' improves the API by a small factor, which is a win given the wide usage iterators will enjoy.

hax commented 2 years ago

I created PR to replace the word from "back" to "last".

I'm also considering separate API form (nextLast() instead of next("last")). The reason I didn't use nextLast() (or nextBack() like rust) in the first place, is because the consideration of generators (https://github.com/tc39/proposal-deiter/issues/9#issuecomment-1143128315) . But I'm ok to reconsider it if it could solve the confusion better.

hax commented 2 years ago

It seems no other comments, so I will merge the PR tomorrow.