Win a copy of Rust Web Development this week in the Other Languages 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Generics: no unique maximal instance exists for type variable

 
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I think this is more advice then a question as I think I now understand what the above message is trying to tell me.
Here is the setup:



Ok, so in my mind and Eclipse this all works fine, but line //EEEK fails big time on the command line.
I think basicly what is happening is that javac donst know what E should be, even though it has been told it will extend Record (which is why I think it should work), E is some object that extends Record, toNetSuiteRecord is returning some object that extends Record. It should all be fine.

I think the only way to fix this is to replace EEEK with:

Then provide the required class.

Can anybody offer any better advice?

For the record I am using generics because Record is just a Super type, it dosnt group the generic Record functionality, it is just there for structure, much like a marker interface, so this was the only may I could think of to offer the conversion for the many different sub types of Record I need.

Thanks
G
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Gavin Tranter wrote:E is some object that extends Record, toNetSuiteRecord is returning some object that extends Record. It should all be fine.


But that doesn't guarantee that toNetSuiteRecord returns the same type of sub-class of Record that E happens to be. What if, say, toNetSuiteRecord returns a SubClassOfRecord, and E is ADifferentSubClassOfRecord? The assignment won't be valid then.

Either your selected solution, or using E super Record instead of E extends Record would make it compile. (I couldn't say which is better - it depends on the rest of the situation).
 
Gavin Tranter
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I thought that E super Record means that E is a super type of Record?

I think you are right in general and I will have to document that as a possibility. However I am only passing in list of one type (ie List<SomeTypeOfRecord>) rather then extends x.
Its a bit of a worry
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Gavin Tranter wrote:I thought that E super Record means that E is a super type of Record?


That's right. Thinking like a compiler: if all you know about a return value is that it's a sub-type of X, then the only reference you can safely assign it to is an X, or any super-type of X.

Edit: perhaps you don't actually need the generic types on the return value? In toNetSuiteRecord, for example, I'm not sure what the R parameter is gaining you. Pretty much anything that calls that method is going to have to assume a Record is getting returned anyway.
 
Gavin Tranter
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not sure I am getting how E super Record would help as the things I am putting into E are subtypes.

On the other hand I think you might be right about not gaining anything from it. If i get a chance I will see if it works any better with just a List<Record> or List<? extends Record>.

Thanks
G
 
reply
    Bookmark Topic Watch Topic
  • New Topic