RFR: 8341782: Allow lambda capture of basic for() loop variables as with enhanced for() [v3]

Archie Cobbs acobbs at openjdk.org
Fri Oct 11 19:50:34 UTC 2024


> 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);
> }

Archie Cobbs has updated the pull request incrementally with one additional commit since the last revision:

  Update copyright year.

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

Changes:
  - all: https://git.openjdk.org/jdk/pull/21415/files
  - new: https://git.openjdk.org/jdk/pull/21415/files/00181f7f..76d5c5e7

Webrevs:
 - full: https://webrevs.openjdk.org/?repo=jdk&pr=21415&range=02
 - incr: https://webrevs.openjdk.org/?repo=jdk&pr=21415&range=01-02

  Stats: 1 line in 1 file changed: 0 ins; 0 del; 1 mod
  Patch: https://git.openjdk.org/jdk/pull/21415.diff
  Fetch: git fetch https://git.openjdk.org/jdk.git pull/21415/head:pull/21415

PR: https://git.openjdk.org/jdk/pull/21415


More information about the compiler-dev mailing list