Skip to content

Support spreading and destructuring of iterators in ES6 #2498

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

Merged
merged 20 commits into from
Apr 8, 2015

Conversation

JsonFreeman
Copy link
Contributor

This adds support for a few things:

  • Spreading an iterator into an array
var array = [0, 1, ...new NumberIterator];
  • Spreading an iterator into a call
function foo(...nums: number[]) { }
foo(...new NumberIterator);
  • Destructuring an iterator into an array
var [a, b, ...c] = new NumberIterator; // a and b have type number, c has type number[]
var d: number, e: number, f: number[];
[d, e, ...f] = new NumberIterator;
function foo([p, q, ...r]: Iterable<number>) {}
foo(new NumberIterator);

I will also add that tuples are relatively unaffected. If you declare something to be a tuple, you can still only assign a tuple to it. And if you have an array binding pattern with no rest element, its implied type is a tuple. So the following does give you an error:

function foo([p, q]) {}
foo(new NumberIterator);

@JsonFreeman JsonFreeman mentioned this pull request Mar 26, 2015
4 tasks
// Normally, in ES6, the implied type of an array binding pattern with a rest element is
// an iterable. However, there is a requirement in our grammar checker that all rest
// parameters be array types. To satisfy this, we have an exception to the rule that
// says the type of an array binding pattern with a rest element is an array type
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's an odd requirement given that the grammar only says this:

RestParameter:
    ...   Identifier   TypeAnnotation_opt

I don't see why, at grammar check time, we'd care if the parameter was an array type or not.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's the type system, not the grammar checker. I will fix the comment

@JsonFreeman
Copy link
Contributor Author

To see a design proposal, see #1900 (comment)

@yuit
Copy link
Contributor

yuit commented Apr 8, 2015

forgot to approve 👍

JsonFreeman added a commit that referenced this pull request Apr 8, 2015
Support spreading and destructuring of iterators in ES6
@JsonFreeman JsonFreeman merged commit 21a5611 into master Apr 8, 2015
@JsonFreeman JsonFreeman deleted the iteratorSpreadDestructure branch April 8, 2015 23:58
@microsoft microsoft locked and limited conversation to collaborators Jun 18, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants