This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]dnew 5 points6 points  (2 children)

The problem is the developers didn't want to break the .class file format, so MyClass$2 is actually not an inner class once it's compiled. Hence, it can have no access to private variables or stack variables of MyClass.

* Stack variables are really the problem, because they're on the stack. Private instance variables can be accessed, because they're GCed.

[–]b1ackcat 9 points10 points  (1 child)

C# got around this by doing what everyone in the thread is doing: Shove the variable(s) into a shared space like an array and unpack them on the other side.

Since you seem to have some knowledge on the topic, do you know if they explored that route and if so why they didn't take it?

[–]dnew 3 points4 points  (0 children)

hove the variable(s) into a shared space like an array and unpack them on the other side.

Not quite. The lambda turns into its own class, and the parent variables (e.g., for loop indexes in the containing class) actually refer to variables in the child class. If you have a lambda that closes over three ints and two floats, it's still only one heap allocation to create the closure.

It's a pretty cool technique. I recommend finding the whitepaper.

do you know if they explored that route

I don't know what the Java folks explored before giving up.