contravariance

How would contravariance be used in Java generics?

纵然是瞬间 提交于 2019-11-27 11:03:16
In Java, covariance allows the API designer to specify that an instance may be generalised as a certain type or any of that type's subtypes. For example: List<? extends Shape> shapes = new ArrayList<Circle>(); // where type Circle extends Shape Contravariance goes the other way. It allows us to specify that an instance may be generalised as a certain type or supertype. List<? super Shape> shapes = new ArrayList<Geometry>(); // where Shape extends Geometry How is Java generic's contravariance useful? When would you choose to use it? Jon Skeet Well, your second example would allow you to write:

Why doesn't delegate contravariance work with value types?

邮差的信 提交于 2019-11-27 08:06:04
This snippet is not compiled in LINQPad. void Main() { (new[]{0,1,2,3}).Where(IsNull).Dump(); } static bool IsNull(object arg) { return arg == null; } The compiler's error message is: No overload for 'UserQuery.IsNull(object)' matches delegate 'System.Func' It works for a string array, but doesn't work for int[] . It's apparently related to boxing, but I want to know the details. The answer given (that there is no variance involving value types) is correct. The reason covariance and contravariance do not work when one of the varying type arguments is a value type is as follows. Suppose it did

Why the concept of “Covariance” and “Contravariance” are applicable while implementing the methods of an interface?

大城市里の小女人 提交于 2019-11-27 07:46:42
问题 The use case is some what like this: public class SomeClass : ICloneable { // Some Code // Implementing interface method public object Clone() { // Some Clonning Code } } Now my question is Why is it not possible to use "SomeClass(As it is derived from object)" as a return type of Clone() method if we consider the Funda's of Covariance and Contravariance ? Can somebody explain me the reason behind this implementation of Microsoft ???? 回答1: A non-broken implementation of interface

Why is parameter in contravariant position?

自古美人都是妖i 提交于 2019-11-27 06:41:17
I'm trying to use a covariant type parameter inside a trait to construct a case-class like so: trait MyTrait[+T] { private case class MyClass(c: T) } compiler says: error: covariant type T occurs in contravariant position in type T of value c I then tried the following but it also didn't work: trait MyTrait[+T] { private case class MyClass[U <: T](c: U) } the error this time is: error: covariant type T occurs in contravariant position in type >: Nothing <: T of type U Could somebody explain why the T is in a covariant position here and suggest a solution for this problem? Thx! This is a

Why covariance does not work with generic method

情到浓时终转凉″ 提交于 2019-11-27 02:06:19
问题 Assume I have interface and class: public interface ITree {} public class Tree : ITree {} As IEnumerable<T> is covariant , the code line below is compiled successfully: IEnumerable<ITree> trees = new List<Tree>(); But when I put it into generic method: public void Do<T>() where T : ITree { IEnumerable<ITree> trees = new List<T>(); } I get compiled error from compiler: Error 1 Cannot implicitly convert type 'System.Collections.Generic.List' to 'System.Collections.Generic.IEnumerable'. An

T must be contravariantly valid

本秂侑毒 提交于 2019-11-27 01:31:42
What is wrong with this? interface IRepository<out T> where T : IBusinessEntity { IQueryable<T> GetAll(); void Save(T t); void Delete(T t); } It says: Invalid variance: The type parameter 'T' must be contravariantly valid on 'MyNamespace.IRepository.Delete(T)'. 'T' is covariant. Consider what would happen if the compiler allowed that: interface IR<out T> { void D(T t); } class C : IR<Mammal> { public void D(Mammal m) { m.GrowHair(); } } ... IR<Animal> x = new C(); // legal because T is covariant and Mammal is convertible to Animal x.D(new Fish()); // legal because IR<Animal>.D takes an Animal

ref and out parameters in C# and cannot be marked as variant

此生再无相见时 提交于 2019-11-27 01:13:14
What does the statement mean? From here ref and out parameters in C# and cannot be marked as variant. 1) Does it mean that the following can not be done. public class SomeClass<R, A>: IVariant<R, A> { public virtual R DoSomething( ref A args ) { return null; } } 2) Or does it mean I cannot have the following. public delegate R Reader<out R, in A>(A arg, string s); public static void AssignReadFromPeonMethodToDelegate(ref Reader<object, Peon> pReader) { pReader = ReadFromPeon; } static object ReadFromPeon(Peon p, string propertyName) { return p.GetType().GetField(propertyName).GetValue(p); }

Generic Variance in C# 4.0

两盒软妹~` 提交于 2019-11-27 01:08:34
问题 Generic Variance in C# 4.0 has been implemented in such a way that it's possible to write the following without an exception (which is what would happen in C# 3.0): List<int> intList = new List<int>(); List<object> objectList = intList; [Example non-functional: See Jon Skeet's answer] I recently attended a conference where Jon Skeet gave an excellent overview of Generic Variance, but I'm not sure I'm completely getting it - I understand the significance of the in and out key words when it

How would contravariance be used in Java generics?

走远了吗. 提交于 2019-11-26 22:19:55
问题 In Java, covariance allows the API designer to specify that an instance may be generalised as a certain type or any of that type's subtypes. For example: List<? extends Shape> shapes = new ArrayList<Circle>(); // where type Circle extends Shape Contravariance goes the other way. It allows us to specify that an instance may be generalised as a certain type or supertype. List<? super Shape> shapes = new ArrayList<Geometry>(); // where Shape extends Geometry How is Java generic's contravariance

Why is parameter in contravariant position?

北慕城南 提交于 2019-11-26 22:15:38
问题 I'm trying to use a covariant type parameter inside a trait to construct a case-class like so: trait MyTrait[+T] { private case class MyClass(c: T) } compiler says: error: covariant type T occurs in contravariant position in type T of value c I then tried the following but it also didn't work: trait MyTrait[+T] { private case class MyClass[U <: T](c: U) } the error this time is: error: covariant type T occurs in contravariant position in type >: Nothing <: T of type U Could somebody explain