RFR: 8341782: Allow lamba capture of basic for() loop variables as with enhanced for()

Rémi Forax forax at openjdk.org
Wed Oct 9 05:05:57 UTC 2024


On Tue, 8 Oct 2024 19:53:56 GMT, Archie Cobbs <acobbs at openjdk.org> wrote:

> This PR changes the compiler to allow basic `for()` loop iteration variables to be captured by lambdas in the loop body, even when their "effectively final" status is invalidated by modifications in the header of the loop.
> 
> This allows code like this to compile:
> 
> for (int i = 1; i <= 3; i++) {
>     Runnable r = () -> System.out.println(i);
> }

Hello Archie, i'm very pleased you are working on this,
this is one issue we (the lambda expert group) wanted to fix but could not deliver in time for Java 8.

-------------

PR Comment: https://git.openjdk.org/jdk/pull/21415#issuecomment-2401322269


More information about the compiler-dev mailing list