Effectively final effective?

Peter Levart peter.levart at gmail.com
Sun Feb 28 10:49:38 PST 2010


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