Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generics Wildcard question

 
Femi Byte
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am wondering whether the statements below are true



Upon looking at Angelika Langer's generics FAQ, she states in the following section:

Is List<Object> a supertype of List<String>?

No, different instantiations of the same generic type for different concrete type arguments have no type relationship.


So, my assumption would be that the answer would be false for each of the above statements.

But later down in the same section of the FAQ, there is this statement:

Compatibility between instantiations of the same generic type exist only among wildcard instantiations and concrete instantiations that belong to the family of instantiations that the wildcard instantiation denotes.


which indicates to me that there is a super-sub type relationship for wildcards.
So which is correct for the examples above?
 
Stephan van Hulst
Bartender
Posts: 6327
78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
List<? extends A> is the supertype of all lists of types that are subtypes of A. Therefore, List<? extends A> is the supertype of List<A>, as well as List<C>, if C extends A.

List<? super A> is the supertype of all lists of types that are supertypes of A. Therefore, List<? super A> is the supertype of List<A>, as well as List<Object>.

Wildcards always indicate supertypes.

[edited 10.000 times because the forum interprets my type arguments as bold text ]
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Regarding the two quotes you posted, there is no contradiction. The first quote applies when both types are concrete, with no wildcards. List<Object> is neither a supertype nor subtype of List<String>. But the second quote applies where at least one of the types does use a wildcard.
 
Femi Byte
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
.
 
Femi Byte
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:List<? extends A> is the supertype of all lists of types that are subtypes of A. Therefore, List<? extends A> is the supertype of List<A>, as well as List<C>, if C extends A.

List<? super A> is the supertype of all lists of types that are supertypes of A. Therefore, List<? super A> is the supertype of List<A>, as well as List<Object>.

Wildcards always indicate supertypes.

[edited 10.000 times because the forum interprets my type arguments as bold text ]


So you're saying that List<? extends A> is a supertype, not subtype of List<A>?
 
Stephan van Hulst
Bartender
Posts: 6327
78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yep!
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think of the IS-A test:

List<A> IS-A List<? extends A> ? - yes
List<? extends A> IS-A List<A> ? - no
 
Stephan van Hulst
Bartender
Posts: 6327
78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is an example:

You can see that I can assign a List<Number> to both List<? extends Number> and List<? super Number> because they are both supertypes of List<Number>. In order to do the reverse, I need to cast.
Note that this code will compile with warnings, but it's perfectly valid.
 
Femi Byte
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your responses below clarify things quite a bit. Thanks for your responses.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic