I am looking for a way to invoke multiple argument methods but using a lambda
construct. In the documentation it is said that lambda
is only usable if it can map to a functional interface.
I want to do something like:
test((arg0, arg1) -> me.call(arg0, arg1));
test((arg0, arg1, arg2) -> me.call(arg0, arg1, arg2));
...
Is there any way one can do this elegantly without defining 10 interfaces, one for each argument count?
Update
I use multiple interfaces extending from a non-method interface and I overload the method.
Example for two arguments:
interface Invoker {}
interface Invoker2 extends Invoker { void invoke(Object arg0, Object arg1);}
void test(Invoker2 invoker, Object ... arguments) {
test((Invoker)invoker, Object ... arguments);
}
void test(Invoker invoker, Object ... arguments) {
//Use Reflection or whatever to access the provided invoker
}
I hope for a possibility to replace the 10 invoker interfaces and the 10 overloaded methods with a single solution.
I have a reasonable use case and please do not ask questions like 'Why would you do such a thing?' and 'What is the problem you are trying to solve?' or anything like that. Just know that I have thought this through and this is a legitimate problem I'm try to solve.
Sorry to add confusion calling it invoker but it is actually what it is called in my current use case (testing constructor contracts).
Basically, as stated above, think about a method that works with a different number of attributes within the lambda
.