GNOME Bugzilla – Bug 694895
Support for covariance and contravariance of generics and restrictions on them
Last modified: 2016-11-30 15:31:06 UTC
1. It would be nice to introduce a covariance/contravariance in types: public void insert<T>(T item, List<? super T> list) { list.add(item); // Ok. If list have type List<G> then T is subtype of G } public void T get_random<T>(List<? extends T> list) { int elem = rand.rand_int (0, list.size); return list.get(elem); // Ok. If list have type List<G> then G is subtype of T } 2. It would be nice to allow restriction on arguments: public abstract MyCollection<C extends Collection<int>> { public MyCollection(C c) { c.add(0); // Ok. C is Collection<int> so we can call add } public C col; } List<int> l = new ArrayList<int>(); MyCollection<List<int>> c = new MyCollection<List<int>>(l); List<int> l2 = c.col; // Ok. c.col have type List<int>
*** This bug has been marked as a duplicate of bug 668474 ***