Skip to content

Redesign for loop #29

Open
Open

Description

@lam2mo

Make it more "pythonic"?

Activity

  1. added this to the v1.0 milestone on Jan 25, 2019
  2. lam2mo commented on May 29, 2019

    @lam2mo
    MemberAuthor

    I think at some point we decided this would be best if we just introduced a range notation for defining integral lists. Example:

    for (i : [1..n]) {
        println(i)
    }
    

    and modify the old iterator-based syntax accordingly:

    for (c : ["A", "B", "C"]) {
        println(c)
    }
    

    I don't think we need full-fledged iterators to make this work because we're not so concerned about performance; we can just generate the full list at the beginning of the loop then use the regular for-each loop in Java.

  3. lam2mo commented on May 29, 2019

    @lam2mo
    MemberAuthor

    In terms of grammar changes, this would remove the (expr '->')? part of the for-loop production in statement and add a new LSB expr '..' expr RSB production for ranges in collection. For type checking, both limits of the range would have to unify to int. For code generation, an ArrayList should be populated by items in the range given.

    Issues:

    1. Should the bounds be inclusive or exclusive? Consistency with zero-based array indices and other languages would suggest inclusive on the lower bound and exclusive on the upper bound, so that for (i : [0..5]) would execute five times and i would take on the values of indices in a five-element array.

    2. Should the range only allow increasing values, or decreasing as well? It shouldn't be hard to allow either; the code generated can examine the bounds at runtime and see which is larger than the other and adjust the loop update accordingly. Note that if you allow both and you keep the inclusivity rules from above (inclusive lower and exclusive upper), then for (i : [n..0]) acts as you might expect.

  4. lam2mo commented on Jan 24, 2020

    @lam2mo
    MemberAuthor

    We should address this in an all-hands meeting to make a decision.

  5. removed this from the v1.0 milestone on Feb 13, 2020
  6. lam2mo commented on Feb 13, 2020

    @lam2mo
    MemberAuthor

    This has been tabled for now.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions