I have written this interface as part of a framework.
public interface CollectionFactory {
public <T> Collection<T> newCollection();
}
But I want the implementer to be able to define the returned type of the collection so they won’t have to cast like:
public interface CollectionFactory<C extends Collection> {
public C newCollection();
}
The problem is that I then lose typesafety on T. I would like it to be
public interface CollectionFactory<C extends Collection> {
public <T> C<T> newCollection();
}
And I don’t want to specify T in advance like so:
public interface CollectionFactory<T, C extends Collection<T>> {
public C newCollection();
}
To the best of my knowledge this is not possible.
Would someone like to surprise me?
Also, just as an appetizer, does anyone know if something similar to this is possible in say… Scala?