Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #20987

Re: Why is that in JDK8: value used in lambda expression shuld be effectively final?

X-Received by 10.66.72.3 with SMTP id z3mr1417368pau.40.1357398111508; Sat, 05 Jan 2013 07:01:51 -0800 (PST)
Received by 10.49.81.72 with SMTP id y8mr9622508qex.42.1357398111445; Sat, 05 Jan 2013 07:01:51 -0800 (PST)
Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!f6no15537698pbd.1!news-out.google.com!s9ni79141pbb.0!nntp.google.com!b8no8680859pbd.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail
Newsgroups comp.lang.java.programmer
Date Sat, 5 Jan 2013 07:01:51 -0800 (PST)
In-Reply-To <c885bedb-977c-49b4-a10a-b9cdd3df1dd9@googlegroups.com>
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 <c885bedb-977c-49b4-a10a-b9cdd3df1dd9@googlegroups.com>
User-Agent G2/1.0
MIME-Version 1.0
Message-ID <1cb8648d-4fe3-40f9-b433-ee027770746a@googlegroups.com> (permalink)
Subject Re: Why is that in JDK8: value used in lambda expression shuld be effectively final?
From Saxo <jeti789@web.de>
Injection-Date Sat, 05 Jan 2013 15:01:51 +0000
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding quoted-printable
Xref csiph.com comp.lang.java.programmer:20987

Show key headers only | View raw


Having to declare the free variable of lambda expression final has also it's advantages. This here is taken from the JDK8 lambda FAQ (http://www.lambdafaq.org/what-are-the-reasons-for-the-restriction-to-effective-immutability):

"The restriction on local variables helps to direct developers using lambdas aways from idioms involving mutation; it does not prevent them. Mutable fields are always a potential source of concurrency problems if sharing is not properly managed; disallowing field capture by lambda expressions would reduce their usefulness without doing anything to solve this general problem."

Question is whether this is intentional or a pretext. Unhappily, it also does not matter since this restriction can be circumvented anyway with the single-element-array trick:

final int sumArray[] = new int[] { 0 };
ints.forEach(i -> {sumArray[0] += i;}); 
println(sumArray[0]); 

Whether you declare sumArray final here or not makes no difference in the matter. 

Your idea with @Share is interesting. There is f.ex. @Immutable in Groovy. In Groovy you can define those things like your @Share annotation through AST transformations and this way make additions to the language. You can also define such AST transformations in Scala where they are called macros.

If you are interested in those things Groovy or Scala may be worth a look :-).

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

Why is that in JDK8: value used in lambda expression shuld be effectively final? jeti789@web.de - 2013-01-02 04:51 -0800
  Re: Why is that in JDK8: value used in lambda expression shuld be effectively final? Steven Simpson <ss@domain.invalid> - 2013-01-02 14:15 +0000
    Re: Why is that in JDK8: value used in lambda expression shuld be   effectively final? Robert Klemme <shortcutter@googlemail.com> - 2013-01-07 23:08 +0100
  Re: Why is that in JDK8: value used in lambda expression shuld be effectively final? Saxo <jeti789@web.de> - 2013-01-02 09:46 -0800
  Re: Why is that in JDK8: value used in lambda expression shuld be effectively final? Saxo <jeti789@web.de> - 2013-01-05 07:01 -0800

csiph-web