I\'m currently looking at closure implementations in different languages. When it comes to Scala, however, I\'m unable to find any documentation on how a closure is mapped to Ja
Unlike Java's anonymous inner classes that are pseudo-closures and cannot modify the variables that appear to be closed into its their environment, Scala's closures are real, so the closure code directly references the values in the surrounding environment. Such values are compiled differently when they are referenced from a closure in order to make this possible (since there's no way for method code to access locals from any activation frames other than the current one).
In contrast, in Java their values are copied to fields in the inner class, which is why the language requires the original values in the enclosing environment to be final
, so they can never diverge.
Because all the Scala function literal's / closure's references to values in the enclosing environment are in the code of the function literal's apply()
method, they don't appear as fields in the actual Function
subclass generated for the function literal.
I don't know how you're decompiling, but the details of how you did so probably explain why you're not seeing any code for the body of the apply()
method.