Closures, too much or too little?
Mark Mahieu
markmahieu at googlemail.com
Tue Nov 24 03:04:28 PST 2009
Yep, that's what I meant by the variable (count) 'becoming final'; the second count++ would cause a compiler error.
Same deal here, as flow analysis would indicate that n is mutated 'after' being used in the closure:
for (int n = 1; n <= 10; n++) {
someList.add(#() n); // compile error
}
Mark
On 24 Nov 2009, at 09:56, Roel Spilker wrote:
> One more thing. If there is a write to count after the assignment to count0, the compiler should give at least a warning. Otherwise the reader of this code could thing the closure would see the modification if the closure is used after the modification.
>
> int count = 0;
> count++;
> Runnable r = #() {doSomethingWith(count);};
> count++;
> r.run();
>
> would desugar to
>
> int count = 0;
> count++;
> final int count0 = count; // generated
> Runnable r = #() {doSomethingWith(count0);};
> count++;
> r.run();
>
> In this scenario, I'd like to get an error or at least a warning that count is modified after the final variable has been assigned.
>
> Roel
>
>
> -----Oorspronkelijk bericht-----
> Van: reinier at zwitserloot.com [mailto:coin-dev-bounces at openjdk.java.net] Namens Reinier Zwitserloot
> Verzonden: dinsdag 24 november 2009 0:41
> Aan: Mark Mahieu
> CC: coin-dev
> Onderwerp: Re: Closures, too much or too little?
>
> Mark, spec-wise, "turning final" is effectively equivalent to the compiler desugaring your source code snippet to:
>
> int count = 0;
> final int count0;
> count++;
>
> count0 = count; //generated
> Runnable r = #() {doSomethingWith(count0);};
>
>
More information about the coin-dev
mailing list