contravariance

Overriding Java generic methods

不问归期 提交于 2019-12-10 19:29:30
问题 I wanted to create an interface for copying an object to a destination object of the same class. The simple way is to use casting: import org.junit.Test; import org.junit.internal.runners.JUnit4ClassRunner; import org.junit.runner.RunWith; @RunWith(JUnit4ClassRunner.class) public class TestGenerics { public static interface Copyable { public void copy(Copyable c); } public static class A implements Copyable { private String aField = "--A--"; protected void innerCopy(Copyable c) { A a = (A)c;

Casting from IEnumerable<Object> to IEnumerable<string>

不打扰是莪最后的温柔 提交于 2019-12-10 13:12:51
问题 Recently I found a very surprising behavior in c#. I had a method which takes IEnumerable<Object> as a parameter and i was passing IEnumerable<string> but it's not possible. While in c# everything can be upcast to Object than why this is not possible? It's totally confusing for me. Please someone clear me on this issue. 回答1: The technical term for this is that generics are invariant in C# 3.0 and earlier. From C#4.0 onward, the cast works. What invariant means is that there is no relationship

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

荒凉一梦 提交于 2019-12-10 11:22:18
问题 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 ? 回答1: .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

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

≯℡__Kan透↙ 提交于 2019-12-10 10:54:21
问题 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

Implementing a method inside a Scala parameterized class with a covariant type

故事扮演 提交于 2019-12-09 22:50:02
问题 I've read a few tutorials including the main Scala documentation regarding method signatures of covariant types. Suppose I have the following abstract class: abstract class List[+A] { def head: A def tail: List[A] def isEmpty: Boolean def add[B >: A](element: B): List[B] protected def printElements: String override def toString: String = "[" + printElements + "]" } My question concerns the signature of the add() method. Why is it necessary to declare it that way? We are passing in a parameter

Contravariance in Expressions

一曲冷凌霜 提交于 2019-12-09 16:06:54
问题 I'm trying to create a Generic Action Delegate delegate void ActionPredicate<in T1, in T2>(T1 t1, T2 t2); and public static ActionPredicate<T,string> GetSetterAction<T>(string fieldName) { ParameterExpression targetExpr = Expression.Parameter(typeof(T), "Target"); MemberExpression fieldExpr = Expression.Property(targetExpr, fieldName); ParameterExpression valueExpr = Expression.Parameter(typeof(string), "value"); MethodCallExpression convertExpr = Expression.Call(typeof(Convert), "ChangeType"

Scala contravariance and covariance

戏子无情 提交于 2019-12-09 07:44:39
问题 I am playing around with the typesystem of scala and found a strange case. I have a strong reason to believe, I don't understand covariant and covariance. This is my problem case: I have two classes, Point and ColorPoint, which is a subclass of Point. class Point(val x : Int, val y : Int) class ColorPoint(x : Int, y : Int, val red : Int, val green : Int, val blue : Int) extends Point(x,y) This class casts B to A, while B should be a supertype of A: class CoVariance[+A]{ def cast[B >: A](x : B

Why the type position of a method is marked as negative?

[亡魂溺海] 提交于 2019-12-09 03:18:51
问题 Sorry I have asked some questions like this one, but I still can't get a clear answer, maybe my bad English and unclear expression puzzled the kind people. When I read the "Type Parameterization" in this article: http://www.artima.com/pins1ed/type-parameterization.html, I see there are some explanation about the type positions: As a somewhat contrived example, consider the following class definition, where the variance of several positions is annotated with ^+ (for positive) or ^- (for

VB.NET Implement Multiple Contravariant interface types

不问归期 提交于 2019-12-08 16:39:07
问题 Basic Question: Given an interface: ICopiesFrom(Of In TModel) where there is no type constraint on the generic argument, can that interface be implemented more than once on the same concrete type using a different type argument without a compiler warning? Background Info: My handle on covariance and contravariance has been increasing in recent years thanks to Mr. Eric Lippert, Google, and many hours of testing / experimenting. In a project I am working on, I have a need to separate different

Type L is in contravariant position in type A => Either[L, B]

你说的曾经没有我的故事 提交于 2019-12-08 07:34:20
问题 I tried to write simple implementation of flatMap for Either sealed trait Either[+L, +R] { def flatMap[B](f: R => Either[L, B]): Either[L, B] = this match { case Left(e) => Left(e) case Right(e) => f(e) } } final case class Right[+A, +B](right: B) extends Either[A, B] final case class Left[+A, +B](left: A) extends Either[A, B] and faced following problem: covariant type L is in contravariant position in type f: R => Either[L, B] of value f, but why is it so? I thought that our type is in