contravariance

C# : Is Variance (Covariance / Contravariance) another word for Polymorphism?

て烟熏妆下的殇ゞ 提交于 2019-11-26 21:30:45
I am trying to figure out the exact meaning of the words Covariance and Contravariance from several articles online and questions on StackOverflow, and from what I can understand, it's only another word for polymorphism . Am I correct with the above statement? Or have I got it wrong ? Jon Skeet It's certainly related to polymorphism. I wouldn't say they're just "another word" for polymorphism though - they're about very specific situations, where you can treat one type as if it were another type in a certain context . For instance, with normal polymorphism you can treat any reference to a

Casting List<T> - covariance/contravariance problem

本小妞迷上赌 提交于 2019-11-26 21:04:55
Given the following types: public interface IMyClass { } public class MyClass : IMyClass { } I wonder how can I convert a List<MyClass> to a List<IMyClass> ? I am not completely clear on the covariance/contravariance topics, but I understand that I cannot just plainly cast the List because of that. I could come up with this trivial solution only; lacking any elegance, wasting resources: ... public List<IMyClass> ConvertItems(List<MyClass> input) { var result = new List<IMyClass>(input.Count); foreach (var item in input) { result.Add(item); } return result; } .... How can you solve it in a more

Understanding Covariance and Contravariance in C# 4.0

旧巷老猫 提交于 2019-11-26 18:09:23
问题 I watched a video about it on Channel 9 but I didn't really understand it much. Can someone please give me a simple example about these that's easy to understand? After that maybe how it would be used in practice? 回答1: You may want to look at this blog, he does a fantastic job of explaining it, but I think it will take more examples to clear it up for people, as this gets into a very hard-to-understand area, but, the quote below from the article sums it up well. http://hestia.typepad.com

How to find the minimum covariant type for best fit between two types?

倖福魔咒の 提交于 2019-11-26 16:52:09
问题 There's IsAssignableFrom method returns a boolean value indicates if one type is assignable from another type. How can we not only test if they are assignable from or to each other, but also know the minimum covariant type for best fit? Consider the following example(C# 4.0) Code // method body of Func is irrelevant, use default() instead Func<char[]> x = default(Func<char[]>); Func<int[]> y = default(Func<int[]>); Func<Array> f = default(Func<Array>); Func<IList> g = default(Func<IList>); g

Generics : List<? extends Animal> is same as List<Animal>?

孤街醉人 提交于 2019-11-26 15:56:03
I am just trying to understand the extends keyword in Java Generics. List<? extends Animal> means we can stuff any object in the List which IS A Animal then won't the following also mean the same thing: List<Animal> Can someone help me know the difference between the above two? To me extends just sound redundant here. Thanks! List<Dog> is a subtype of List<? extends Animal> , but not a subtype of List<Animal> . Why is List<Dog> not a subtype of List<Animal> ? Consider the following example: void mySub(List<Animal> myList) { myList.add(new Cat()); } If you were allowed to pass a List<Dog> to

How do I return a reference to something inside a RefCell without breaking encapsulation?

北城余情 提交于 2019-11-26 14:43:00
I have a struct that has inner mutability. use std::cell::RefCell; struct MutableInterior { hide_me: i32, vec: Vec<i32>, } struct Foo { //although not used in this particular snippet, //the motivating problem uses interior mutability //via RefCell. interior: RefCell<MutableInterior>, } impl Foo { pub fn get_items(&self) -> &Vec<i32> { &self.interior.borrow().vec } } fn main() { let f = Foo { interior: RefCell::new(MutableInterior { vec: Vec::new(), hide_me: 2, }), }; let borrowed_f = &f; let items = borrowed_f.get_items(); } Produces the error: error[E0597]: borrowed value does not live long

Why are contravariant parameter types in Java not allowed for overriding?

谁说胖子不能爱 提交于 2019-11-26 14:22:01
问题 When overriding a method of a superclass, Java allows the return type to be covariant. Why are contravariant parameter types in contrast not allowed when overriding methods? 回答1: Because that's called overloading. In particular, the return type type can be covariant because it is not considered when overloading, and it therefore still matches the superclass or interface's implementation. Parameters are considered when overloading. You very well might have an optimization with Number

T must be contravariantly valid

白昼怎懂夜的黑 提交于 2019-11-26 14:05:54
问题 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. 回答1: 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

Why doesn&#39;t delegate contravariance work with value types?

戏子无情 提交于 2019-11-26 13:59:28
问题 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. 回答1: The answer given (that there is no variance involving value types) is correct. The reason covariance and

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

℡╲_俬逩灬. 提交于 2019-11-26 09:37:01
问题 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