An anonymous Java inner class can refer to final data in the inclusion method and all data (including mutable) in the enclosing class. Thus, methods in anonymous classes cannot change the values โโof variables in the environment method, but they can change the values โโof elements in the enclosing class.
C ++ lambda can refer to all data (including mutable) in a closing function, and if it is nested inside a member function, then it can do the same for the data of the enclosing class. The exact degree of dependence on the encompassing area (s) is declared by the programmer, so it is explicit, not hidden.
This makes them very similar, but the Java function treats local variables / parameters in methods differently, on the principle that they should not be changed outside the method, especially in a language that traditionally uses threads so carelessly.
Compare with C # lambdas, which have no restrictions, and all dependencies are implicit. This makes them, by far, the least verbose of these functions (also better type inference helps them). But on the other hand, they invalidate all simple streaming rules, i.e. It is no longer necessarily true that local variables are "on the thread stack" and therefore never require locking before access.
Daniel Earwicker
source share