Effectively final effective?
Reinier Zwitserloot
reinier at zwitserloot.com
Sun Feb 28 19:38:02 PST 2010
Agreed; variable initializers in basic for loops can get special treatment
if need be (cannot be captured in closures at all). They are somewhat unique
beasts already in the spec.
--Reinier Zwitserloot
2010/2/28 Peter Levart <peter.levart at gmail.com>
> On Sunday 28 February 2010 04:15:25 pm John Nilsson wrote:
> > On Sun, Feb 28, 2010 at 11:29 AM, Peter Levart <peter.levart at gmail.com
> >wrote:
> > > for(int i = 0; i < 3; i++) {
> > >
> > > final int ii = i;
> > >
> > > funs.add(()->ii);
> > >
> > > }
> > >
> > > or:
> > >
> > > for(int i = 0; i < 3; i++) {
> > >
> > > @SuppressWarnings("for loop index capture")
> > >
> > > funs.add(()->i);
> > >
> > > }
> > >
> > > Depending whether you want your program to print 012 or 333
> > > (respectively).
> > >
> > > That's what I think is a good enough solution to this problem.
> > >
> > > Regards, Peter
> >
> > Then someone comes along and "refactors" the code from
> >
> > for(int i = 1; i <= 3; i++) //Can I get this right?
> > funs.add(()->i);
> >
> > to
> >
> > for(int i : in(1,2,3))
> > fins.add(()->i);
> >
> > Accidentally breaking indexing semantics.
>
> Yeah, I see.
>
> Then maybe it is best to just disallow basic for loop index capture.
> Enhanced for loop variable
> is another story and it is usually effectively final anyway.
>
> Peter
>
> >
> >
> > BR,
> > John
>
More information about the lambda-dev
mailing list