Closed lukehutch closed 4 years ago
I got an answer on StackOverflow about the right way to do this:
In fact given the response, it might be useful to create a lambda factory method that you just pass a SAM object instance into to turn it into a lambda.
Sorry for the late reply and thanks for the link and analysis. I will look into this when I get the time. Feel free to issue a Pull Request in case you have already solved it. /Anders
I @lukehutch
Are sure this does not work, if you pass the instance as the first parameter to the invoke method, just like with java.lang.Method's invoke? I will check myself monday.
Sorry, it has been too long, and I don't remember any of the complex details anymore. Hopefully the links I provided will help.
I have tested your code, and the problem is that you are creating a lambda of an instance method, but are invoking without any instance. You must invoke like this:
z.invoke_for_void(new Z(), Integer.valueOf(1));
Ah, OK, thanks and sorry for the trouble.
Given the following interface and implementing class:
The following code gives
java.lang.AbstractMethodError: Receiver class [...]$Z$$Lambda$82/0x0000000800200040 does not define or inherit an implementation of the resolved method abstract invoke_for_void(Ljava/lang/Object;)V of interface com.hervian.lambda.Lambda.
However changing class
Z
to the following works:Lambdas in Java are typically non-static, since they are created from SAM interfaces, so it would be nice to have this working in the original form.
You would probably have to somehow replicate Java's logic used to determine if a lambda or inner class captures any variables, in order to determine whether you need to allocate a new instance of the defining class every time the method is invoked (maybe disallow this case), or whether you can allocate just one instance, and reuse it for each invocation.