Closed hax closed 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.
"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.)
It's the "next" method name that makes it confusing, not just the magic string.
imo the word "next" always means "the one after" in english; the next thing can never be the previous thing.
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".
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.
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.
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.
It seems no other comments, so I will merge the PR tomorrow.
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:
next("last")
next("from-last")
next("backend")
next("back-end")
next("from-back")
next("from-end")
next("from-backend")
next("right")
next("from-right")
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".