There is nothing complicated in converting one class of methods to function pointers, but one thing is missing: lambda expressions are not just functions, but closures . The difference is that closures can capture external variables. Consider the following example in pseudo Java:
public Adder makeAdder(double startNumber) { return #{ int number -> number + startNumber} } ... int startNumber = 5; Adder add5 = makeAdder(startNumber); add5.invoke(4);
In this example, the lambda function created by calling makeAdder () refers to a variable that was defined outside of this lambda. That's why it is called “closure” - they are “closed” over their free variables (in this case, over startNumber). To cope with such situations, closures must contain both a pointer to a function and a pointer to its environment . Thus, you get some data structure that has a method and at least one variable. But isn't this an object definition in OOP? So, what is the reason for creating a new type of object if you can make it an instance of an anonymous class?
However, some other optimizations for such anonymous classes can be performed. The working document that you pointed out mentions some of them, for example, outputting and using efficient final variables (although this is mainly done to allow lambdas to the JVM in principle, and not to optimize the code). A produced anonymous class can also be finalized, and most JVMs already have good optimization for the final classes and classes.
Other improvements may also concern environmental references - there are many options.
ffriend
source share