You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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:
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.
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.
Activity
lam2mo commentedon May 29, 2019
I think at some point we decided this would be best if we just introduced a range notation for defining integral lists. Example:
and modify the old iterator-based syntax accordingly:
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.
lam2mo commentedon May 29, 2019
In terms of grammar changes, this would remove the
(expr '->')?part of the for-loop production instatementand add a newLSB expr '..' expr RSBproduction for ranges incollection. For type checking, both limits of the range would have to unify toint. For code generation, anArrayListshould be populated by items in the range given.Issues:
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 andiwould take on the values of indices in a five-element array.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.lam2mo commentedon Jan 24, 2020
We should address this in an all-hands meeting to make a decision.
lam2mo commentedon Feb 13, 2020
This has been tabled for now.