Hi Eric,

my take inline:

On 02/12/2020 17:34, Milles, Eric (TR Technology) wrote:

Traditional "for" (first example) and ARM "try" (last example) support local variable declarations that are scoped to the statement.  In light of the upcoming "instanceof" enhancement in Java, I was thinking about possible alternatives for declaring local variables that have statement scope.

for (int i = ...; ...) {

  // i available

}

// i unavailable


Good: Well established & clear syntax

for (x in y index i) { // from Gosu (http://gosu-lang.github.io/docs.html) -- an alternative to using eachWithIndex

}


Not fond of: Syntax looks confusing and forced to me, and I see no advantage over eachWithIndex...

Something along that line could make sense to me, if we loose the "index" keyword, and allow for multiple iterations in parallel, e.g.:

for(t0 in tables0, t1 in tables1, i2, i3+7, i4+13:2) {
    /*
        t0, t1, i, and k all advance each for loop step, if we assume i is a hidden loop variable starting at zero and being incremented by 1 each loop, as follows:
        t0 = tables0[i]
        t1 = tables1[i]
        i2 = i
        i3 = i +7
        i4 = 2*i + 13

        One question would be, if we allow tables0.size() != tables1.size() - not sure about that, but it would feel Groovy compatible to return null if the smaller collection has been exhausted...
    */
}


if (x instanceof T t) { // from Java 14+

}


Good: Might be useful to be able to make the type explicit in some cases, so yes, also valuable besides being Java compatible.

if (def x = ...) { // tests Groovy truth in this form; may be wrapped in parens to check something else about "x"

}


try (def ac = ...) {

}


Good: Both useful, and if it works for for, it should work in similar places.

Cheers,
mg



Reply via email to