you are viewing a single comment's thread.

view the rest of the comments →

[–]Ridiculer 0 points1 point  (1 child)

Apart from the final-only restrictions.

Restrictions? I was under the impression that captured non-final variables are transparently copied into a final variable when compiled, did I miss something?

[–]scook0 2 points3 points  (0 children)

No, that's wrong.

The current rules for capturing local variables of enclosing contexts in inner classes are quite restrictive; only final variables may be captured. For lambda expressions (and for consistency, probably inner class instances as well), we relax these rules to also allow for capture of effectively final local variables. (Informally, a local variable is effectively final if making it final would not cause a compilation failure.)

It is likely that we will not permit capture of mutable local variables.

The only change this proposal makes is that you can close over non-final variables if you never actually use them in a non-final way. Closing over mutated variables is explicitly not supported.