Skip to content

Implement for...of syntax for Iterables #576

Closed

Description

@joshwd36

As more collections are added, it would be useful to implement the for...of syntax for Iterables, and adding iterables to the String, Array, and Map types. ECMAScript specification.

This code should now work and give the expected result:

let a = [1, 2, 3];
for (i of a) {
    console.log(i);
}

The expected output is:

1
2
3

I'm happy to have a go at this myself, although as this will be my first time adding syntax features, I'm wondering whether I should wait for #559 to land or just go right ahead with the current lexer?

Activity

  1. added
    A-Enhancement
    on Jul 18, 2020
  2. Lan2u commented on Jul 19, 2020

    @Lan2u

    You could make a branch-based from #559 if you wanted to get started right away, I expect that most of the work regarding this would be in the parser (syntax/parser/statement/iteration) and exec (exec/iteration/mod.rs).

    In terms of the lexer I think you will just need to add a new 'of' keyword (syntax/ast/keyword)

  3. dvtkrlbs commented on Aug 30, 2020

    @dvtkrlbs

    I can take a look at this. Does this include the for-in statement too?

  4. joshwd36 commented on Aug 30, 2020

    @joshwd36
    ContributorAuthor

    I can take a look at this. Does this include the for-in statement too?

    I've made a start on it already, but haven't been able to work on it recently. I should have more time this week though so I'll try and finish it then, if not I'm happy to transfer what I've done over to you.

  5. jcdickinson commented on Aug 31, 2020

    @jcdickinson
    Contributor

    Dropping #671 for whoever wants to take this on. See my comments there, as there is a tricky design question. My current thoughts are:

    pub enum IterableForLoopKind {
        In,
        Of
    }
    struct InnerIterableForLoop {
        kind: IterableForLoopKind,
        variable: SingleVarLetOrConstDecl, // or something
        object: Node,
        body: Node
    }

    Note that #670 would also be closed, as for..in is simply a for..of with a built-in iterator.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A-Enhancement

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions