I hope to find Java generation experts here. Say you have some kind of typed class:
public interface SomeClass<T> {
void doSomething(final T t);
}
There is also a function that provides you with an instance T
for an instance SomeClass<T>
:
public static class Retriever {
public <T> T get(final SomeClass<T> c) {
return null;
}
}
Now let's say you have a collection SomeClass<?>
and a retriever:
final List<SomeClass<?>> myClasses = null;
final Retriever myRetriever = null;
We cannot do the following:
for (final SomeClass<?> myClass : myClasses) {
myClass.doSomething(myRetriever.get(myClass));
}
Now my question is: do I need Java support for local type determination? Sort of:
<T> for (final SomeClass<T> myClass : myClasses) {
myClass.doSomething(myRetriever.get(myClass));
}
Here the type is T
bound to a for loop. We define T
to get rid of the wildcard ?
. It. The introduction T
should allow us to write down the desired cycle cycle, as indicated above.
FWIW, . ?
T
.
for (final SomeClass<?> myClass : myClasses) {
workAround(myRetriever, myClass);
}
public static <T> void workAround(final Retriever myRetriever, final SomeClass<T> myClass) {
myClass.doSomething(myRetriever.get(myClass));
}
?