aspose file tools
The moose likes Java in General and the fly likes Generics: no unique maximal instance exists for type variable Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Generics: no unique maximal instance exists for type variable" Watch "Generics: no unique maximal instance exists for type variable" New topic
Author

Generics: no unique maximal instance exists for type variable

Gavin Tranter
Ranch Hand

Joined: Jan 01, 2007
Posts: 333
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
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 3795
    
    1

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

Joined: Jan 01, 2007
Posts: 333
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

Joined: Apr 06, 2010
Posts: 3795
    
    1

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

Joined: Jan 01, 2007
Posts: 333
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generics: no unique maximal instance exists for type variable
 
Similar Threads
Generics
generics
Generics questions
Generics
Generics