contravariance

Why method defined like “cons[B >: A](v: B)” accepts argument of type which is not supertype of A?

放肆的年华 提交于 2019-11-30 15:00:13
I am studying variance in scala right now, and I think I have a good understanding of contravariance. For example given trait List[-A] , I know that List[Int] is a supertype of List[AnyVal] . But say that I have the following trait: trait List[+A] { def cons(hd: A): List[A] } Why is cons parameter type wrong? Why it is necessary to have def cons[B >: A](v: B): List[B] ? For example: val animal_list: List[Animal] = List(tiger, dog) if we call: animal_list.cons(tiger) since Tiger <: Animal , doesn't cons ran into problem? Since B is Tiger and A is Animal and B >: A is not true. TeWu Why is cons

C# variance annotation of a type parameter, constrained to be value type

我与影子孤独终老i 提交于 2019-11-30 01:14:55
It is possible in C# to add variance annotation to type parameter, constrained to be value type: interface IFoo<in T> where T : struct { void Boo(T x); } Why is this allowed by compiler if variance annotation makes completely no sense in a such situation? Why this is allowed by compiler since variance annotation make completely no sense in a such situation? It's allowed by the compiler because I never even considered that someone might try to do that when I added the variance rules to the C# 4.0 compiler. Compiler warnings and errors are features , and in order for a feature to be implemented,

Generic wildcards in variable declarations in Scala

依然范特西╮ 提交于 2019-11-29 17:33:51
问题 In Java I might do this: class MyClass { private List<? extends MyInterface> list; public void setList(List<MyImpl> l) { list = l; } } ...assuming that ( MyImpl implements MyInterface ) of course. What is the analog for this in Scala , when using a Buffer ? import java.lang.reflect._ import scala.collection.mutable._ class ScalaClass { val list:Buffer[MyInterface] = null def setList(l: Buffer[MyImpl]) = { list = l } } This (of course) doesn't compile - but how do I declare the list variable

What are good reasons for choosing invariance in an API like Stream.reduce()?

本秂侑毒 提交于 2019-11-29 16:35:25
问题 Reviewing Java 8 Stream API design, I was surprised by the generic invariance on the Stream.reduce() arguments: <U> U reduce(U identity, BiFunction<U,? super T,U> accumulator, BinaryOperator<U> combiner) A seemingly more versatile version of the same API might have applied covariance / contravariance on individual references to U , such as: <U> U reduce(U identity, BiFunction<? super U, ? super T, ? extends U> accumulator, BiFunction<? super U, ? super U, ? extends U> combiner) This would

C# delegate contravariance with lambda expression [duplicate]

我的梦境 提交于 2019-11-29 15:12:18
This question already has an answer here: Can’t assign to delegate an anonymous method with less specific parameter type 3 answers The second test method below does not compile (cannot convert lambda expression to target type D1 ). Does that mean that (non-generic) delegate contravariance does not work with lambda expressions? [TestFixture] public class MyVarianceTests { private abstract class Animal {} private class Tiger : Animal {} private delegate Type D1(Tiger tiger); private static Type M1(Animal animal) { return animal.GetType(); } [Test] public void ContravariantDelegateWithMethod() {

How to make generic class that contains a Set of only its own type or subtypes as Children?

六月ゝ 毕业季﹏ 提交于 2019-11-29 14:25:37
abstract class Animal { } class Mammal : Animal { } class Dog : Mammal { } class Reptile : Animal { } class AnimalWrapper<T> where T : Animal { public ISet<AnimalWrapper<T>> Children { get; set; } } class Program { public static void Main(string[] args) { var foo = new AnimalWrapper<Mammal>(); foo.Children = new HashSet<AnimalWrapper<Mammal>>(); var child = new AnimalWrapper<Dog>(); foo.Children.Add(child); } } This obviously doesn't compile because of foo.Children.Add(child); I'm not sure if the above code is the most clear way to demonstrate what I want to do, so I will try to explain in

How to make a generic class with inheritance?

旧城冷巷雨未停 提交于 2019-11-29 12:30:50
How can I make the following code work? I don't think I quite understand C# generics. Perhaps, someone can point me in the right direction. public abstract class A { } public class B : A { } public class C : A { } public static List<C> GetCList() { return new List<C>(); } static void Main(string[] args) { List<A> listA = new List<A>(); listA.Add(new B()); listA.Add(new C()); // Compiler cannot implicitly convert List<A> listB = new List<B>(); // Compiler cannot implicitly convert List<A> listC = GetCList(); // However, copying each element is fine // It has something to do with generics (I

C# variance annotation of a type parameter, constrained to be value type

房东的猫 提交于 2019-11-28 22:03:07
问题 It is possible in C# to add variance annotation to type parameter, constrained to be value type: interface IFoo<in T> where T : struct { void Boo(T x); } Why is this allowed by compiler if variance annotation makes completely no sense in a such situation? 回答1: Why this is allowed by compiler since variance annotation make completely no sense in a such situation? It's allowed by the compiler because I never even considered that someone might try to do that when I added the variance rules to

Why aren't there many discussions about co- and contra-variance in Haskell (as opposed to Scala or C#)?

回眸只為那壹抹淺笑 提交于 2019-11-28 17:23:36
I know what covariance and contravariance of types are. My question is why haven't I encountered discussion of these concepts yet in my study of Haskell (as opposed to, say, Scala)? It seems there is a fundamental difference in the way Haskell views types as opposed to Scala or C#, and I'd like to articulate what that difference is. Or maybe I'm wrong and I just haven't learned enough Haskell yet :-) There are two main reasons: Haskell lacks an inherent notion of subtyping, so in general variance is less relevant. Contravariance mostly appears where mutability is involved, so most data types

Simple examples of co and contravariance

纵然是瞬间 提交于 2019-11-28 15:58:56
Could someone provide me simple C# examples of convariance, contravariance, invariance and contra-invariance (if such thing exists). All samples I've seen so far was just casting some object into System.Object . Could someone provide me simple C# examples of convariance, contravariance, invariance and contra-invariance (if such thing exists). I have no idea what "contra-invariance" means. The rest are easy. Here's an example of covariance: void FeedTheAnimals(IEnumerable<Animal> animals) { foreach(Animal animal in animals) animal.Feed(); } ... List<Giraffe> giraffes = ...; FeedTheAnimals