X-Received: by 10.66.78.70 with SMTP id z6mr8876985paw.5.1357379400477; Sat, 05 Jan 2013 01:50:00 -0800 (PST) Received: by 10.49.38.194 with SMTP id i2mr9498622qek.30.1357379400412; Sat, 05 Jan 2013 01:50:00 -0800 (PST) Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!f6no13789113pbd.1!news-out.google.com!s9ni72755pbb.0!nntp.google.com!f6no13789110pbd.1!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: comp.lang.java.programmer Date: Sat, 5 Jan 2013 01:50:00 -0800 (PST) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=77.1.205.153; posting-account=QuOrbQoAAADk1Ytmd4kkfWK7_Nh1pnw_ NNTP-Posting-Host: 77.1.205.153 References: <9f030e71-96ab-4ead-9690-4369f4a19aa9@googlegroups.com> <0680c1e0-16cb-4791-8a5f-95a3ff2bcba8@googlegroups.com> User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: <6f020a38-4566-4739-8ec7-a85d02a68d86@googlegroups.com> Subject: Re: Why is that in JDK8: value used in lambda expression shuld be effectively final? From: Saxo Injection-Date: Sat, 05 Jan 2013 09:50:00 +0000 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Xref: csiph.com comp.lang.java.programmer:20972 Am Freitag, 4. Januar 2013 22:32:38 UTC+1 schrieb Steven Simpson: > On 03/01/13 14:45, Saxo wrote: >=20 > I'm still interested in seeing an example that you would normally write= =20 > with free variables, demonstrating that lambdas are inadequate for the=20 > things you'd like to do.=20 Groovy: def list =3D [1, 2, 3] def sum =3D 0 list.each { sum +=3D it } println(sum) Kotlin: val list =3D arrayListOf(1, 2, 3) var sum =3D 0; list.forEach { i -> sum +=3D i } println(sum) Scala: val list =3D List(1, 2, 3) =20 var sum =3D 0 =20 list.foreach(sum +=3D _) println(sum) Works in all these languages that support closures (and I guess in any othe= r language that supports closures as well), but the same does not work with= JDK8 lambdas. Reality can be tough... There is always the workaround of defining a single= element array final: final int sumArray[] =3D new int[] { 0 }; ints.forEach(i -> {sumArray[0] +=3D i;});=20 println(sumArray[0]);=20 I wrote in my blog: The lambda specification (JSR 335) also says so explicitly: "For both lambd= a bodies and inner classes, local variables in the enclosing context can on= ly be referenced if they are final or effectively final. A variable is effe= ctively final if it is never assigned to after its initialization.". And I provided the link to JSR 335: http://cr.openjdk.java.net/~dlsmith/jsr= 335-0.6.1/B.html I took some effort in my blog to describe the matter and provided the links= to the respective resources on the JDK8 lambda homepage :-). OTOH, JDK8 lambdas are still enormously useful. No doubt about that. An ime= nse amount of pre-JDK8 boilerplate code can be thrown out of myriads of sys= tems, frameworks and applications. It's a big step ahead. Nevertheless, it = can be said that JDK8 lambdas don't go all the way to be called true closur= es. Apparently, JDK8 lambdas can be assigned to a Runnable. Maybe this was = a deliberate choice to stay backwards compatible.=20 -- Oliver