You should try to reuse the LambdaMetaFactory instead of generating your own proxy, - generating a proxy means you have a way to define a class in module that do not own, so you have to use lookup.defineClass (or worst unsafe.defineClass), so you need to generate an invokedynamic - you can generate the proxy at compile time but in that case, you loose the fact that lambdas reduce of disk footprint. - JDK lambdas use a lightweight classloader so their code is unloaded if the lambda is GCed, something which is hard to emulate with your own proxy, - JDK lambdas consider captured values as really final fields trusted by the VM, again something hard to emulate.
so (2) seems to be a better option. cheers, Rémi ----- Mail original ----- > De: "Daniel Sun" <realblue...@hotmail.com> > À: "dev" <d...@groovy.incubator.apache.org> > Envoyé: Mardi 30 Janvier 2018 01:14:25 > Objet: About the callable native lambda > Hi all, > > I'm trying to implement the callable native lambda(e.g. > `Function<Integer, Integer> f = e -> e + 1; assert 3 == f(2);`), and there > are 2 ideas come to my mind: > > 1) Generate a proxy implementing the FunctionInterface(e.g. `Function`, > `Consumer`, etc.) and a `Callable` interface(maybe we should create a new > one), the proxy will delegate invocations of `call(Object... args)` method > of `Callable` to the method of the FunctionInterface. As you know, `f(2)` is > actually `f.call(2)`, so we need the method `call`. > 2) Transform `f(2)` to `f.apply(2)`, we can get the target method name by > the type of FunctionInterface. To make it clear, let's have a look at two > example: `Function`'s method is `apply`, `Consumer`'s method is `accept`. > > I prefer the first way, but I want to get advice from you :-) > > In addition, have you any idea about the issue[1]? If you do not know > off the top of your head, I'll have to investigate when I have some spare > time. Any help is appreciated! > > Cheers, > Daniel.Sun > > [1] > https://github.com/apache/groovy/blob/native-lambda/src/main/java/org/codehaus/groovy/classgen/asm/sc/StaticTypesLambdaWriter.java#L136 > > > > -- > Sent from: http://groovy.329449.n5.nabble.com/Groovy-Dev-f372993.html