0

I need to pass a series of Class type arguments with associated callbacks to a method without defining additional classes or interfaces.

I tried something like:

void foo(Map<Class, Function> callbacks){
    for(Class clazz : callbacks.keySet()){
        if(!(callbacks.get(clazz) instanceof Function<clazz, clazz>)){
            System.out.println("callback type mismatch");
        }
    }
    //(own logic)
}

or

void foo(List<<T>Map<Class<T>, Function<T, T>>> callbacks){
    ...
}

which is erroneous. Any ideas how to overcome this issue (or why it is not possible without additional classes)?

ggorlen
  • 44,755
  • 7
  • 76
  • 106
Lubomír Grund
  • 145
  • 1
  • 13
  • I don't get why you need to know class? Can you do just something like void foo(Function ...callback){...} – shapkin Oct 19 '18 at 11:30
  • 1
    The book *Effective Java* has a whole chapter on what Bloch calls "Typesafe hetrogeneous containers" which is basically what you're trying to construct here. – Daniel Pryden Oct 19 '18 at 12:17

0 Answers0