Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Generics compilation problem  RSS feed

 
Gert Demol
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I'm experimenting with generics and I encountered this problem. Lines 16 and 17 don't compile. Apparently the compiler prevents me from adding an A or an Object while I specified List<? super B>. Why doesn't this compile? Why can I add a B or a C but not A or Object?
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gert Demol wrote:Apparently the compiler prevents me from adding an A or an Object while I specified List<? super B>.

Yes. A List<? super B> could be a List<B>, List<A>, or List<Object>. The compiler doesn't know which, based on the declared type. So since it could be a List<B>, you can't put an A or an Object in it. But you can always put a B or C in the list, because those will fit into any of the possible types of the list.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!