Here is what I would like to write in my java code:
private <A extends Action<R>, R extends Result> MyType<A,R> member;
This is invalid syntax however. So I end up writing:
private MyType<? extends Action<? extends Result>, ? extends Result> member;
But this disregard the fact that both classes derived from Result
are the same. My class methods all enforce this relationship, so I can be sure that MyType enforces it, but I still have to unsafely typecast member
in some instances.
More details
Here is the precise version of what I want to do, although it is much more criptic:
I wish I could do:
private <A extends Action<R>, R extends Result>
Map< Class<A>, ActionHandler<A,R> > handlers;
Instead I have to do:
private Map< Class< ? extends Action<? extends Result> >,
ActionHandler<? extends Action<? extends Result>,
? extends Result> > handlers;
My methods enforce the desired relationship and look like that:
public <A extends Action<R>, R extends Result> void addHandler(
ActionHandler<A, R> handler ) {
handlers.put( handler.getActionType(), handler );
}
I would like the following method:
public <A extends Action<R>, R extends Result> ActionHandler<A, R>
findHandler( A action ) {
return handlers.get( action.getClass() );
}
But this doesn't work: I have to add a cast and a @SuppressWarnings("unchecked")
.
Things I tried
I tried creating a new class just for that purpose :
public class MyMap <A extends Action<R>, R extends Result> extends
HashMap< Class<A>, ActionHandler<A,R> > { ... }
MyMap< ?, ? > handlers;
But it didn't work, I still need a cast.