Reputation: 153
I want to receive a function in parameter and call it with one parameter as:
public static <T> T foo(Callable<T> func) { return func.call("bar"); }
But it call doesn't take any parameter. Any idea on how can I do this?
No matter how much I search, I dont find anything that help me...
Upvotes: 1
Views: 200
Reputation: 49552
The call
method defined in Callable
has no parameters defined so you cannot pass anything to it.
Depending on what you want to do exactly you can write your own interface for that:
public interface CallableWithParameters<T> {
public T call(Object... arguments);
}
Then you call it in different ways:
call();
call(someObject);
call("someString", 42);
Upvotes: 3
Reputation: 328598
A Callable<T>
only has one method: T call()
.
If you want something different, you will need to use a different parameter type, for example:
public interface CallableWithString<T> {
T call(String arg); //Assuming you want a string argument
}
Then your function can do:
public static <T> T foo(CallableWithString<T> func) {
return func.call("bar");
}
Upvotes: 5