On 28.04.21 03:43, Christopher Smith wrote:
This would be both expensive and problematic with static mode, which is
what I use almost exclusively for production code.

On Tue, Apr 27, 2021, 20:26 Remi Forax <fo...@univ-mlv.fr
<mailto:fo...@univ-mlv.fr>> wrote:
[...]
    It may be simpler to have a conversion from any instance of a
    functional interface to a Closure,
    with closure.call() calling the abstract method of the functional
    interface.

    This will require to check at runtime if a class implement a
    functional interface, to wrap the instance in a Closure.
    It would be cool if the annotation @FunctionalInterface was declared
    with a retention RUNTIME, so the check to know if an instance is a
    lambda or not will be easy.
    But wait, it's already the case, someone thought about that in the
    lambda expert group :)

If this way is expensive solely depends on the implementation. Of course
it is always more expensive than "no more conversion needed"

I want to give you a different angle your original question. Let us put
our method calls with Closures in two categories:

* Fixed Scope
Where we have attached code blocks with the same restrictions as
functional interfaces in Java, only that they would be instances of
Closure. They may capture and modify variables of the surrounding
*static* scope. *collect*, may support different arities, but important
really is that it does not mess with the resolution. This is an example
of Fixed Scope

* Altering Scope
Where the method to be called is using the resolving strategy and or
delegate to change the resolution of variables. *with* requires to
change the resolution strategy, so it can't take a functional interface
and is an example of Altering Scope.


Which means there are methods with scope alteration and such without,
and only those without can be using Functional interfaces.


On a practical level there is the current problem (unless Daniel "fixed"
that and I did not notice. As I cannot follow the project as much
anymore, this is entirely possible) that calling a method with
functional interface using a code block in Groovy will still create a
Closure, then convert the Closure with a Proxy to the interface. This is
of course less efficient then calling into the interface implementation
directly.

bye Jochen

Reply via email to