covariance

<out T> vs <T> in Generics

末鹿安然 提交于 2019-11-26 03:03:33
问题 What is the difference between <out T> and <T> ? For example: public interface IExample<out T> { ... } vs. public interface IExample<T> { ... } 回答1: The out keyword in generics is used to denote that the type T in the interface is covariant. See Covariance and contravariance for details. The classic example is IEnumerable<out T> . Since IEnumerable<out T> is covariant, you're allowed to do the following: IEnumerable<string> strings = new List<string>(); IEnumerable<object> objects = strings;

IDictionary<TKey, TValue> in .NET 4 not covariant

痴心易碎 提交于 2019-11-26 02:57:18
问题 The IDictionary<TKey, TValue> in .NET 4 / Silverlight 4 does not support covariance, i.e. I can\'t do a IDictionary<string, object> myDict = new Dictionary<string, string>(); analog to what I can do with IEnumerable<T> s now. Probably boils down to the KeyValuePair<TKey, TValue> not being covariant either. I feel that covariance should be allowed in dictionaries at least for the values. So is that a bug or a feature? Will it ever come, maybe in .NET 37.4? UPDATE (2 years later): There will be

Demonstrate covariance and contravariance in Java? [closed]

。_饼干妹妹 提交于 2019-11-26 02:50:46
Please show a good example for covariance and contravariance in Java. Covariance: class Super { Object getSomething(){} } class Sub extends Super { String getSomething() {} } Sub#getSomething is covariant because it returns a subclass of the return type of Super#getSomething (but fullfills the contract of Super.getSomething()) Contravariance class Super{ void doSomething(String parameter) } class Sub extends Super{ void doSomething(Object parameter) } Sub#doSomething is contravariant because it takes a parameter of a superclass of the parameter of Super#doSomething (but, again, fullfills the

What is a covariant return type?

橙三吉。 提交于 2019-11-26 02:27:27
问题 What is a covariant return type in Java? In object-oriented programming in general? 回答1: Covariant return, means that when one overrides a method, the return type of the overriding method is allowed to be a subtype of the overridden method's return type. To clarify this with an example, a common case is Object.clone() - which is declared to return a type of Object . You could override this in your own class as follows: public class MyFoo { ... // Note covariant return here, method does not

Covariance and IList

余生长醉 提交于 2019-11-26 02:09:59
问题 I would like a Covariant collection whose items can be retrieved by index. IEnumerable is the only .net collection that I\'m aware of that is Covariant, but it does not have this index support. Specifically, I\'d like to do this: List<Dog> dogs = new List<Dog>(); IEnumerable<Animal> animals = dogs; IList<Animal> animalList = dogs; // This line does not compile Now, I\'m aware of why this is a problem. List implements ICollection that has an Add method. By up casting to IList of Animals, it

still confused about covariance and contravariance & in/out

十年热恋 提交于 2019-11-26 02:06:24
问题 ok i read a bit on this topic on stackoverflow, watched this & this, but still a bit confused about co/contra-variance. from here Covariance allows a \"bigger\" (less specific) type to be substituted in an API where the original type is only used in an \"output\" position (e.g. as a return value). Contravariance allows a \"smaller\" (more specific) type to be substituted in an API where the original type is only used in an \"input\" position. i know it has to do with type safety. about the in

Demonstrate covariance and contravariance in Java? [closed]

别说谁变了你拦得住时间么 提交于 2019-11-26 01:51:10
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 5 years ago . Please show a good example for covariance and contravariance in Java. 回答1: Covariance: class Super { Object getSomething(){} } class Sub extends Super { String getSomething() {} } Sub#getSomething is covariant because it returns a subclass of the return type of Super#getSomething

Understanding Covariant and Contravariant interfaces in C#

ぃ、小莉子 提交于 2019-11-26 01:35:18
问题 I\'ve come across these in a textbook I am reading on C#, but I am having difficulty understanding them, probably due to lack of context. Is there a good concise explanation of what they are and what they are useful for out there? Edit for clarification: Covariant interface: interface IBibble<out T> . . Contravariant interface: interface IBibble<in T> . . 回答1: With <out T> , you can treat the interface reference as one upwards in the hierarchy. With <in T> , you can treat the interface

Why doesn&#39;t the example compile, aka how does (co-, contra-, and in-) variance work?

℡╲_俬逩灬. 提交于 2019-11-26 01:25:24
问题 Following on from this question, can someone explain the following in Scala: class Slot[+T] (var some: T) { // DOES NOT COMPILE // \"COVARIANT parameter in CONTRAVARIANT position\" } I understand the distinction between +T and T in the type declaration (it compiles if I use T ). But then how does one actually write a class which is covariant in its type parameter without resorting to creating the thing unparametrized ? How can I ensure that the following can only be created with an instance

java generics covariance

纵然是瞬间 提交于 2019-11-26 00:29:11
问题 I am having trouble understanding the following article: http://www.ibm.com/developerworks/java/library/j-jtp01255.html Under, Generics are not covariant the author states, Because ln is a List, adding a Float to it seems perfectly legal. But if ln were aliased with li, then it would break the type-safety promise implicit in the definition of li -- that it is a list of integers, which is why generic types cannot be covariant. I can\'t understand the part where it says \"if ln were aliased