-
Notifications
You must be signed in to change notification settings - Fork 34
Iterator.prototype.to and a new protocol #36
Comments
One more argument: methods like |
You can pass the iterator directly into your subclass if that's what you want; i don't think we need to be subclassing friendly when |
@ljharb did you see examples? The main concern here is the readability of code, from left to right, without constant search in the code where what is passed, mixed operators and different names of generating methods. Now, this proposal has it only for arrays ( |
The way to convert to an object is I'd say that you'd either need the pipeline proposal (at which point, |
See #17. |
@ljharb I already wrote the same, please, re-read it one more time: the main concern in the readability. |
@bakkot it's not the same, however, it's related, yes, thanks. |
I don't think such a protocol, nor an ever-expanding list of "toFoo" methods, should be part of this proposal. I think this proposal should continue with toArray only, and a new proposal can be made for either of those. |
|
I didn't insist on adding it to this proposal. But this proposal includes |
It seems to me that the responses so far have not addressed what zloirock actually proposed. He’s not suggesting a proliferation of |
Array is JavaScript's fundamental collection type; it is privileged in various ways, e.g. destructuring syntax, indexed property access, etc. Hopefully you can understand how including the ability to convert to it, as a first-class method, is a high priority for the committee. A future proposal could work on some generic collection creation/collecting/transduction protocol. That would be evaluated independently of the value of toArray(). It is not part of this proposal. |
@zloirock I agree with the last comment, Array is very special so only adding But I also agree |
It could look like a new proposal, but since this proposal includes
Iterator.prototype.toArray
method, seems, it should be opened here and, maybe, be a part of this proposal.So, this proposal contains
.toArray
, by why it's limited only to arrays? Instead of adding methods for each collections type (.toObject
,.toSet
, etc.), we could add one:.to(Collection)
and a protocol for that.Instead of
we could use a pipeline operator:
But it's mixin of different operators and readable not very good.
In my opinion, the chaining of methods could be better:
Adding
.toArray
to this proposal is adding the same, but only for arrays, more other, we have an additional simple way of conversion to arrays - spread operator.So, how should work
.to
method? It's just passingthis
to the method of the passed constructor, the name of this method is a part of this protocol. So, what could be used as this protocol?.from
method.At this moment, it's available of
Array
,%TypedArray%
, onObservable
proposal, available proposal for adding.from
method toSet
,Map
,WeakSet
,WeakMap
.But it missed at least on
Object
(here used.fromEntries
) andURLSearchParams
..fromIterable
method or@@fromIterable
well-known symbol.More other, since here used iterables protocol, it could be added not only to
Iterator.prototype
but also to prototypes of all iterables, but it's not a part of this proposal...So, something like that:
The text was updated successfully, but these errors were encountered: