contravariance

Contravariant method argument type

浪子不回头ぞ 提交于 2019-12-07 09:16:18
问题 wiki Contravariant_method_argument_type says overriding method has the subtyping rule as function type, but no language except one support contravariant argument type. I also not able to come up with any idea of benefit to use that. example: class AnimalShelter { Animal getAnimalForAdoption() { ... } void putAnimal(Animal animal) { ... } } class CatShelter extends AnimalShelter { @Overriding Cat getAnimalForAdoption() { return new Cat(); } @Overriding void putAnimal(Object animal) { … } } My

How is the datatype of type parameter decided in covariance and contravariance?

好久不见. 提交于 2019-12-07 05:59:26
问题 I was reading the book Java Generics and Collections By Maurice Naftalin, Philip Wadler, and within the first two chapters I ended up in having my head messed up with doubts. I was not able to figure out the answers. In the call: public static <T> void copy(List<? super T> dst, List<? extends T> src) { for (int i = 0; i < src.size(); i++) { dst.set(i, src.get(i)); } } List<Object> objs = Arrays.<Object>asList(2, 3.14, "four"); List<Integer> ints = Arrays.asList(5, 6); Collections.copy(objs,

Expression.Convert doesn't throw InvalidOperationException for invariant value type parameters?

只愿长相守 提交于 2019-12-07 04:19:13
问题 Expression.Convert generally throws in InvalidOperationException when "No conversion operator is defined between expression.Type and type." The return type parameter of Func<> is covariant for reference types. // This works. Func<SomeType> a = () => new SomeType(); Func<object> b = a; It isn't covariant for value types. Variance applies only to reference types; if you specify a value type for a variant type parameter, that type parameter is invariant for the resulting constructed type. //

Covariance and Contravariance in C#

五迷三道 提交于 2019-12-06 18:35:05
问题 I will start by saying that I am Java developer learning to program in C#. As such I do comparisons of what I know with what I am learning. I have been playing with C# generics for a few hours now, and I have been able to reproduce the same things I know in Java in C#, with the exception of a couple of examples using covariance and contravariance. The book I am reading is not very good in the subject. I will certainly seek more info on the web, but while I do that, perhaps you can help me

Shouldn't Covariance/Contravariance allow this in C# 4.5?

左心房为你撑大大i 提交于 2019-12-06 06:04:47
private Dictionary<Type, List<IDataTransferObject>> dataStore = new Dictionary<Type, List<IDataTransferObject>>(); public void Insert<T>(T dto) where T : IDataTransferObject { if (!dataStore.ContainsKey(typeof(T))) { dataStore.Add(typeof(T), new List<T>()); } dataStore[typeof(T)].Add(dto); } The above code gives me a compile error on the dataStore.Add line because it doesn't like me trying to assign a List<T> to a List<IDataTransferObject> . Since my method restricts T to only IDataTransferObject's shouldn't the covariance/contravariance stuff in .Net 4 allow this code? I know I can change it

Efficiently Obtain IReadOnlyDictionary<int, Animals> from Dictionary<int, Fleas>

亡梦爱人 提交于 2019-12-06 04:34:59
public class Flea : Animals {...} var fleas = new Dictionary<int, Flea>(); public IReadOnlyDictionary<string, Animal> Animals => fleas.ToDictionary(pair => pair.Key, pair => (Animal)pair.Value); Q Is there a more efficient way to obtain Animals from fleas ? .NET supports covariance in interfaces, delegates, generic types and arrays. The interface or type has to specify it's covariant though with the out keyword. You can write IEnumerable<Animal> animals=new List<Flea>(); or var dict=new Dictionary<int,Flea>{ [1]=new Flea() }; IEnumerable<Animal> animals=dict.Values; This works because

Valid type casting of both covariant and contravariant class at runtime in Scala

只谈情不闲聊 提交于 2019-12-05 17:45:27
I wrote a class implementing the command design pattern : class MyCommand[-T, +R](val name: String, val execute: T => R) , prepared two command and stored it in a MutableList: val commands = new mutable.MutableList[MyCommand[Nothing, Any]] commands += new MyCommand[String, String]("lower", s => s.toLowerCase()) commands += new MyCommand[Date, Long]("time", d => d.getTime) Then I have two data to be executed: val data = Array("StRiNG", new Date()) The problem for me is that I don't know how to determine which datum is applicable to the command: data.foreach { d => commands.foreach { c => //

Contravariant method argument type

邮差的信 提交于 2019-12-05 15:55:32
wiki Contravariant_method_argument_type says overriding method has the subtyping rule as function type, but no language except one support contravariant argument type. I also not able to come up with any idea of benefit to use that. example: class AnimalShelter { Animal getAnimalForAdoption() { ... } void putAnimal(Animal animal) { ... } } class CatShelter extends AnimalShelter { @Overriding Cat getAnimalForAdoption() { return new Cat(); } @Overriding void putAnimal(Object animal) { … } } My question is: Is contravariant argument type of overriding method any of good use? if yes, where it is?

Benefits of contravariance in IComparer & IEqualityComparer interfaces

狂风中的少年 提交于 2019-12-05 10:40:29
On the msdn page on contravariance I find a quite interesting example that shows "benefits of contravariance in IComparer" First they use a fairly odd base & derived classes: public class Person { public string FirstName { get; set; } public string LastName { get; set; } } public class Employee : Person { } I can already say that its a bad example cause no class ever just inherits a base class without adding at least a little something of its own. Then they create a simple IEqualityComparer class class PersonComparer : IEqualityComparer<Person> { public bool Equals(Person x, Person y) { .. }

How is the datatype of type parameter decided in covariance and contravariance?

◇◆丶佛笑我妖孽 提交于 2019-12-05 08:24:40
I was reading the book Java Generics and Collections By Maurice Naftalin, Philip Wadler, and within the first two chapters I ended up in having my head messed up with doubts. I was not able to figure out the answers. In the call: public static <T> void copy(List<? super T> dst, List<? extends T> src) { for (int i = 0; i < src.size(); i++) { dst.set(i, src.get(i)); } } List<Object> objs = Arrays.<Object>asList(2, 3.14, "four"); List<Integer> ints = Arrays.asList(5, 6); Collections.copy(objs, ints); assert objs.toString().equals("[5, 6, four]"); During call to the function 'copy': 1st parameter: