Hi,
what I'm trying to do is send a generic method(filter) inside generic object(ItemDelegate) to another generic method(getItem). The problem is that the second method(getItem) can not infer the correct type.
// Generic object
public class ItemDelegate<T> {
public <T> T filter() {
return null;
}
}
// Generic method (receiver):
public static <T> T getItem(ItemDelegate<T> delegate) {
T item = delegate.filter();
//... do something
return item;
}
// Usage in code:
getItem(new ItemDelegate<String>() {
public String filter() {
return "Hi!";
}
}
this code generates a compile error in getItem:
type parameters of T cannot be determined; no unique maximal instance exists for type variable T with upper bounds T,java.lang.Object
Can this even be done in java or is there a better way.
Thanks.