Re: Feedback on ForStatement
Christopher Smith <[email protected]>
| Newsgroups | gmane.comp.lang.groovy.devel |
|---|---|
| Message-ID | <CAG+8NVo5aw92g1khqFH7Vd0B768tyAVKczfzwWnKxsnQXrU+kg@mail.gmail.com> |
How does one distinguish between an absent value and a value of null (e.g., as a list element)? Christopher Smith On Fri, Feb 6, 2026, 10:12 Milles, Eric (TR Technology) via dev < dev-GSC0n/0aZo1d/SJB6HiN2Ni2O/[email protected]> wrote: > I tried to make minimal changes when adding index variable to > ForStatement. It seemed that getValueVariable() and getIndexVariable() was > the cleanest extension of getVariable() (now deprecated). One option > versus adding a constructor would be to add a "forS" factory overload to > GeneralUtils that supports classical "for (int i = 0; i < n; ++i) ..." > without making reference to the dummy variable. > > A bolder approach would be to separate "for (x in y)" from "for (int i = > 0; i < n; ++i)" as distinct statement classes. Maybe they extend from > ForStatement to keep visitors happy. > > > When getIndexVariable() and getValueVariable() were introduced, I designed > them so you could use them equally. That is, they return null when no > index or value variable was given. To that end, I would not want to see > hasValueVariable() and hasIndexVariable() introduced. You should be able > to do any "if (hasValueVariable()) f(getValueVaruable())" with > "Optional.ofNullable(getValueVaruable()).ifPresent(v -> f(v))" or of course > "var v = getValueVariable(); if (v != null) f(v);". > > In summary, I'd favor a light tough for a refactoring; add as little as > possible to the statement class. Unless you are willing to separate the > abstractions of collection and counting loops. > > Eric M. >