Re: Delegation and generics craziness
Tom Anderson wrote:
I may be missing something here, but why is it not just Map<K, V>?
tom
Well, I did that up thread, but I did it quickly and then thought about
it afterwards while I was finishing posting.
What the OP actually has is a constructor that says:
public ExceptionOnDuplicateKeyMap(Map<? extends K, ? extends V>
delegate) {
this.delegate = (Map<K,V>) delegate;
}
So to keep that, I had to cast. But even though the compiler accepted
it, I'm not sure it's correct. For example, if you have a
ExeptionOnDuplicateKeyMap of:
ExceptionOnDuplicateKeyMap<ParentA,ParentB> m //...
you can actually assign some other type with his constructor:
= new ExceptionOnDuplicateKeyMap<ParentA, ParentB>(
new ExceptionOnDuplicateKeyMap<ChildA, ChildB>() );
which doesn't seem kosher to me. (Assume there's a no argument
constructor for the above example to compile. I don't think that
changes anything.) The type of "m" seems like it should be
<ParentA,ParentB> exactly, and not any subclass, because generics don't
inherit. But that's actually what you have, a subclass of ParentA and
ParentB inside "m". So I was wondering if the internal (delegate) type
of Map<?,?> might actually be correct and more type safe. Rather than
what I posted initially.
Generics are weird enough that figuring out the semantics can be hard
for me even if the syntax is correct. They tend to have tricky corners.
I think the type of "m" should be a upper bound, because that what you
actually have, but that's not what the syntax says.