participate


Generics - Contravariance confusion
<<   Back to Forum  |   Give us Feedback
This topic has 1 reply on 1 page.
RGibson
Posts:90
Registered: 3/26/98
Contravariance confusion   
Jun 2, 2003 5:22 AM

 
I mostly get the new variance ideas, but I've come across a case that won't compile and I don't understand why:
    // Array test
    Object[=] array1 = new Object[5];
    Integer[=] array2 = new Integer[5];
    array1 [0] = new Integer(1);
    array2 [0] = new Integer(2);
    Integer[-] array3;
    array3 = array1;
    array3 = array2;
    array3 [0] = new Integer(3);
 
    // This is the List equivalent?
    List<=Object> list1 = new ArrayList<Object>();
    List<=Integer> list2 = new ArrayList<Integer>();
    list1.add (new Integer(1));
    list2.add (new Integer(2));
    List<-Integer> list3;
    list3 = list1;
    list3 = list2;
    list3.add (new Integer (3));

The first half (dealing with arrays) all compiles fine. However, I get a compile error on the very last line:
Reference to add is ambiguous, both method add(E) in java.util.Collection<=-java.lang.Integer> and method add(E) in java.util.List<-java.lang.Integer> match

I don't understand this - is it a bug in the supplied libraries or do not I not understand contravariance properly?
 
gafter
Posts:669
Registered: 6/25/98
Re: Contravariance confusion   
Jun 2, 2003 11:27 AM (reply 1 of 1)  (In reply to original post )

 
I mostly get the new variance ideas, but I've come
across a case that won't compile and I don't
understand why:
    List<-Integer> list3;
    list3.add (new Integer (3));

...I get a compile error on the very last
line:
Reference to add is ambiguous, both method add(E) in
java.util.Collection<=-java.lang.Integer> and method
add(E) in java.util.List<-java.lang.Integer>
match

I don't understand this - is it a bug in the supplied
libraries or do not I not understand contravariance
properly?

Looks like a bug in the compiler. Reported as 4872698.
 
This topic has 1 reply on 1 page.
Back to Forum
 
Read the Developer Forums Code of Conduct

Click to email this message Email this Topic

Edit this Topic
  
 
 
Forums Statistics
    Users Online : 27
  • Guests : 138

About Sun forums
  • Sun Forums is a large collection of user generated discussions. It is here to help you ask questions, find answers, and participate in discussions.

    Check out our guide on Getting started with Sun Forums for a full walkthrough of how to best leverage the benefits of this community.

Powered by Jive Forums