• 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

generics problem

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

Hope I got the right forum - let me know if this is the wrong place.

Here's my problem: in the code below the Client has a compile time error:
Type mismatch: cannot convert from Joiner<capture#1-of ?> to Joiner<File>

My understanding of <?> is 'we don't know what type yet' so what I can't understand is why <file> can't be a type of <?>
By a similar token I've tried making hte return type of the abstract method in the enum factory <Object> - still won't work, you get compile time error in the enum factory that <File> is an incompatable return type.

Perhaps I'm missing some fundamental concept here but I've been googling it all day and I'm totally at a loss - can anyone explain this to me ??

Many thanks in advance.

Phil

 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The abstract method in the JoinerFactory enum declares that it returns a Joiner<?>, even though the CSVJoiner instance of JoinerEnum returns a more specific type, the public interface only exposes the method that returns a Joiner<?>, that's why you're getting the compiler error. If you declare the abstract method to return a Joiner<File>, the error goes away. But that takes away the option for you to return a different type from another enum instance.

One workaround is to use a version of the Typesafe Enum Pattern:

 
phil mitchel
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Garrett - still a bit confused though:

The abstract method in the JoinerFactory enum declares that it returns a Joiner<?>,



- yep so isn't that saying, I'm going to return a Joiner of some type ?

in which case...

is retunring some type - in this case it happens to be of type <File>
- what am I missing here ?

I think from what you're syaing I can infer that <File> does not have an 'isa' relationship with <?> - is this correct ?
 
Sheriff
Posts: 22815
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your enum specifies the return type is Joiner<?>; while in practice this will be Joiner<File>, the compiler does not know that. As far as the compiler is concerned, a Joiner<Integer> could be returned; the ? means that it can be anything.

Since "anything" does not match "File", you will have to cast the Joiner<?> to Joiner<File> (with all dangers if you decide to return a Joiner<String> later), or go for Garrett's solution.
 
phil mitchel
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ah, ok think I got it: the compiler only sees the signature of the abstract method, it doesn't see that the CSVJoiner enum overides the getInstance method and returns the <File> so complains because <?> could be anything. Will look into the Typesafe enum suggested.

thanks for the help !
 
Wanna see my flashlight? How about 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