What is the meaning of this?
Joshua Bloch
jjb at google.com
Thu Jan 28 16:19:07 PST 2010
Peter,
On Thu, Jan 28, 2010 at 1:46 PM, Peter Levart <peter.levart at gmail.com>wrote:
>
>
> Local variables are perfectly thread-safe:
>
> void method()
> {
> final #int(int) factorial = #(int i)(i == 0 ? 1 : i * factorial.(i -
> 1));
> }
>
Yes, this variant is thread-safe, *assuming it's legal*. I'm not sure if it
is. This isn't:
public static void main(String[] args) {
final int i = i + 1;
}
Junk.java:3: variable i might not have been initialized
final int i = i + 1;
^
As I pointed out previously, other variants (static and instance fields)
aren't thread-safe, and I'm afraid people would be equally likely to use
them. More importantly, evaluating this in the enclosing context is
perverse in Java. In every context where this is legal today, it refers to
the innermost instance. It would be inconsistent to do otherwise for
lambdas, and we would need a very good reason to knowingly create this
inconsistency. I haven't heard any such reason. There is, however, a real
disadvantage to making this refer to the enclosing instance: people will
streamline existing code that uses anonymous classes, and could easily end
up with code that doesn't compile or (worse) compiles but misbehaves because
the meaning of this would be different in the new (lambda) and old
(anonymous class) code.
Josh
More information about the lambda-dev
mailing list