I know it's possible to add multiple constraints to a Generic class definition, e.g.:
class Example<I extends Object & Comparable<Object>>{}
But I want a generic (MyGeneric) that takes another generic (SomeGeneric<T>) as its type parameter, and to constrain the type parameter (T) of that generic (e.g. T extends SomeClass).
Important, I need to know the types of both SomeGeneric and SomeClass from inside the class (G and T need to both be bound). For example, imagine something like this:
class MyGeneric<G extends SomeGeneric<T>, T extends SomeClass>
{
public G returnSomeGenericImpl(){}
public T returnSomeClassImpl(){}
}
Question: The above works, but I would prefer if my class had only one type parameter, to make life easier for implementers of my class. Is there a way of doing this?
Something like this would be nice (but this particular code is incorrect):
class MyGeneric<G extends SomeGeneric<T extends SomeClass>>
{
public G returnSomeGenericImpl(){}
public T returnSomeClassImpl(){}
}
If I wasn't clear, I'll gladly try to clarify my intent.
T, how should the compiler guess it? If there's only a single option, then it's easy, but there usually isn't. And if you don't care about the specific type, then why not use a bounded wildcard type (or a local type parameter on the method).<G extends SomeGeneric<SomeClass>>or even<G extends SomeGeneric<? extends SomeClass>>, but the type ofT(?in this case) is not known.public T returnSomeClassImpl(){}new Whatever<SomeGeneric<T>,T>(), we're just repeatingT. if it's not possible it's not possible, that was the point of the questionG extends SomeGeneric<T>toSomeGeneric<T>, then maybe you can get rid ofGaltogether?