• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Different behavior in generic interface declarations

 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry about the topic of this post, I couldn't think of a more descriptive name. I can't seem to figure out why this piece of code works (albeit with a warning):


when this one generates a compiler error:


It seems to me that they are both passing an Object as an argument to a method that takes a more specific type, and yet one works and the other doesn't. I know it has to do with the interface being specified as the raw type in the first example, and I suspect type-erasure plays a part (as it does with all strange behavior regarding generics). But I can't convince myself of any good argument.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You may be over thinking it.

A plain Resolver has a resolve(Object) method.

A Resolver<String> has a resolve(String) method.

You can assign (with a warning) a Resolver<String> to a Resolver variable, and once you've done so, it's just as if you've assigned a String to an Object variable: the compiler treats the thing as if the variable's type is all that matters.

So in the first case, you're calling resolve(Object) on an Object, and it works.

In the second case, you're calling resolve(String) on an Object, and it doesn't.
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ernest Friedman-Hill:
You may be over thinking it.

A plain Resolver has a resolve(Object) method.

A Resolver<String> has a resolve(String) method.

You can assign (with a warning) a Resolver<String> to a Resolver variable, and once you've done so, it's just as if you've assigned a String to an Object variable: the compiler treats the thing as if the variable's type is all that matters.

So in the first case, you're calling resolve(Object) on an Object, and it works.

In the second case, you're calling resolve(String) on an Object, and it doesn't.



I can accept that explanation, so if I write it this way, is there an implicit cast from the Object that was passed as a parameter to an Integer?
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes - and you are right, it has to do with erasure.

In the byte code, r will invariably have a resolve(Object) method, which inside it contains the cast to Integer.
 
And inside of my fortune cookie was this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic