Win a copy of Transfer Learning for Natural Language Processing (MEAP) this week in the Artificial Intelligence and Machine Learning 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
  • Tim Cooke
  • Paul Clapham
  • Devaka Cooray
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Liutauras Vilda
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Piet Souris
Bartenders:
  • salvin francis
  • Carey Brown
  • Frits Walraven

Self-Referencing Generic Arguments

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm having trouble using generics that are a self-references as arguments. What I would like to understand is why this sample code doesn't compile.
If "T" extends Class<T>, why can I not use Class<T> for an argument that accepts "T"?



This is what intellij says but I don't understand it.

reason: no instance(s) of type variables(s) exist so that Class<T> conforms to T
inference variable T has incompatible bounds:
equality constraints: T
lower bounds: Class<T>
 
Marshal
Posts: 25436
65
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi JJ, welcome to the Ranch!

I like that question! At first my answer was "Hmmm... that's interesting...." But what Eclipse says about that is:

Eclipse wrote:The method bar(T) in the type Test is not applicable for the arguments (Class<T>)



By the way that's referring to the call bar(clazz), which it wasn't obvious to me that Intellij was referring to that line. So now it's a lot easier.

The bar method needs a parameter of type T. But you're passing it a reference of type Class<T>, which is a supertype of type T. So it's just the same as if you had a method buz(String) and you passed it an Object.

Make sense?
 
Jj Varg
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:

The bar method needs a parameter of type T. But you're passing it a reference of type Class<T>, which is a supertype of type T. So it's just the same as if you had a method buz(String) and you passed it an Object.

Make sense?

Wow, thanks for the answer! I understand why it doesn't work. What I don't understand is, I thought extends binds the generic so even the supertype is accepted.
For example, this compiles fine.

If T is actually a subtype, then I'm assuming java is doing extra work to make stuff like  String usable in these more normal examples.
 
Paul Clapham
Marshal
Posts: 25436
65
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another good question. Now you're into type inference, I think. When the compiler sees a type variable like T, it tries to "infer" what actual type it represents. Unless there's something which tells it specifically, that is. And in your example there isn't.

So in your second example the compiler sees "takeString(String)" as the signature for line 3. And that's acceptable because String extends String. So it decides that "T" in this case means "String".

But in your first example the compiler sees "bar(Class<T>)" as the signature for line 4. But here it can't decide that "T" in this case means "Class<T>", because that still doesn't resolve an actual non-generic type for "T".

Hope this makes sense too. When generics first arrived in Java, more that a decade ago, somebody wrote a "Generics FAQ" which was, I don't remember, probably over 100 pages long. That's probably still out there on the web and maybe this question is covered there.
 
Jj Varg
Greenhorn
Posts: 3
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, it does! The difference you point out makes things clear, and I'll look into getting that document when I'm encountering anymore confusing issues with generics.
 
Arthur, where are your pants? Check under this tiny ad.
Try Free Java/.NET Libraries for Word Excel PowerPoint and PDF
htttp://www.e-iceblue.com/free-apis.html
    Bookmark Topic Watch Topic
  • New Topic