1

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...

1
  • 1
    I don't understand. You want to call a function that doesn't take any argument, with an argument? Commented Aug 25, 2012 at 16:57

2 Answers 2

5

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");
}
Sign up to request clarification or add additional context in comments.

Comments

3

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.