Win a copy of Emmy in the Key of Code this week in the General Computing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

Compiler error with generics and bounded wildcards

 
Ranch Hand
Posts: 74
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys. I'm reviewing my summaries from the chapter of generics of OCP Study Guide by Jeanne and Scott and pracicing some exercices I came up with the exercise below that I can't totally grasp why doesn't it compile.



The error message is:
error: incompatible types: CAP#1 cannot be converted to Number
for(Number n : list){
              ^
 where T is a type-variable:
   T extends Object declared in method <T>make(List<? extends T>)
 where CAP#1 is a fresh type-variable:
   CAP#1 extends T from capture of ? extends T

It's weird for me because here Test.<Number>make(l); I'm saying that T shoud be taken as Number and then here make(List<? extends T>) I say that the wildcard should extend Number so I don't know what's wrong with this for(Number n : list){.

If I change make(List<? extends T> by make(List<? extends Number> it does compile but in regards what I can "read" of the code at first it should compile.

Please, can someone show me what I'm not finding out?. Thanks as always!
 
Bartender
Posts: 3590
151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you using java 8? It runs fine for me on java 10. I am not aware though of any changes in this respect. Also works is the simple 'Test.make(l);'. Even 'Test.<Double>make' functions. The <T> is not used here.
 
author & internet detective
Posts: 39582
781
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pablo,
The code you posted does compile. I think you are asking why this doesn't compile?



If so, it is because of the generic method.  This means it can be any type.


So the compiler can't assume T is a Number
 
Saloon Keeper
Posts: 10746
229
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Jeanne figured out, the compiler error you showed us does not result from the code you posted. The error message says you declared list as List<? extends T>, but in the code you posted you declared it as List<? extends Number>.
 
Pablo Napoli
Ranch Hand
Posts: 74
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone and sorry for the late reply. Yes, I was wrong with the code that I posted. The line that I wanted to show you should be: "public static <T> void make(List<? extends T> list){". So again I'm gonna paste the code below.



Error message:
Test2.java:5: error: incompatible types: CAP#1 cannot be converted to Number
       for(Number n : list){
                      ^
 where T is a type-variable:
   T extends Object declared in method <T>make(List<? extends T>)
 where CAP#1 is a fresh type-variable:
   CAP#1 extends T from capture of ? extends T
1 error

So what I want to say is:
1-  Test2.<Number>make(l); : I'm indicating that T should be taken as Number.

2- public static <T> void make(List<? extends T> list){ : As T is taken as Number, I could translate it as: public static <Number> void make(List<? extends Number> list){

3 for(Number n : list){ : I don't understand why it does not compile because I'm looping a List referring each element with a Number type.

So this is my question and I tried to be more clear than before.

Thanks again to all those who replied my post.
 
Stephan van Hulst
Saloon Keeper
Posts: 10746
229
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Pablo Napoli wrote:I'm indicating that T should be taken as Number.


The main() method knows that, but the make() method does not.
 
If you live in a cold climate and on the grid, incandescent light can use less energy than LED. Tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!