contravariance

Covariance and Contravariance on the same type argument

╄→гoц情女王★ 提交于 2019-12-18 15:22:26
问题 The C# spec states that an argument type cannot be both covariant and contravariant at the same time. This is apparent when creating a covariant or contravariant interface you decorate your type parameters with "out" or "in" respectively. There is not option that allows both at the same time ("outin"). Is this limitation simply a language specific constraint or are there deeper, more fundamental reasons based in category theory that would make you not want your type to be both covariant and

How are co- and contra-variance used in designing business applications?

心不动则不痛 提交于 2019-12-17 22:41:24
问题 I know about using co- and contravariance in the standard library (e.g. collections and trait Function ) I wonder how co- and contravariance are used in design of "real world" business applications. 回答1: The classic example is functions, taking the Scala interface for a function with a single argument: trait Function1[-T1, +R] Which is contravariant (the - ) for the argument, and covariant (the + ) for the return type. Why? Imagine you have these classes: class Timelord { ... } class Doctor

Simple examples of co and contravariance

淺唱寂寞╮ 提交于 2019-12-17 21:42:14
问题 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 . 回答1: 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)

Why is Function[-A1,…,+B] not about allowing any supertypes as parameters?

为君一笑 提交于 2019-12-17 15:28:01
问题 I believe one can define covariance (at least, for objects) as 'the ability to use a value of a narrower (sub) type in place of a value of some wider (super) type', and that contravariance is the exact opposite of this. Apparently, Scala functions are instances of Function[-A1,...,+B] for contravariant parameter types A1, etc. and covariant return type, B. While this is handy for subtyping on Functions, shouldn't the above definition mean I can pass any supertypes as parameters? Please advise

Scala anonymous function genric variance issues

江枫思渺然 提交于 2019-12-13 05:15:01
问题 I'm on the road to learn Scala and I'm having a hard time understanding contravariants, covariants, invariance, etc. From Why doesn't the example compile, aka how does (co-, contra-, and in-) variance work? I have learned how functions can be considered subtypes of another function. (Really useful to know!) The code below is what I believe are the important pieces to solving my puzzle. I have extracted parts that I think would add unneeded complexity to the problem. According to the example I

How is IEnumerable<T> Contra-variant?

一曲冷凌霜 提交于 2019-12-12 11:22:53
问题 This post (http://blogs.msdn.com/b/brada/archive/2005/01/18/355755.aspx) says IEnumerable<T> is Contra-variant. However type T is co-variant because it is an out parameter. So in what context is IEnumerable<T> Contra-variant ?? Hope I am not confusing! Thanks for the answers in advance! 回答1: IEnumerable isn't contra-variant. It's covariant. From MSDN (IEnumerable<(Of <(T>)>) Interface) we have that: Type Parameters out T The type of objects to enumerate. This type parameter is covariant .

Error: Covariant type A occurs in contravariant position

柔情痞子 提交于 2019-12-12 08:00:48
问题 I was trying to write an immutable Matrix[A] class. I want the class to be covariant on A but when I put + in front of A compiler starts complaining about some operations in the class. Following is a relevant subset of my Matrix class (The actual class is some 5 times bigger than the following subset): class Matrix[+A] private(val contents: Vector[Vector[A]])(implicit numericEv: Numeric[A]) extends ((Int, Int) => A) with Proxy { import numericEv._ import Prelude._ // delegate `equals` and

Create open instance delegate via reflection

我的梦境 提交于 2019-12-11 19:34:00
问题 Why does the following Delegate.CreateDelegate produce a runtime ArgumentException? static class Module1 { public static void Main() { MyDataObject mdo = new MyDataObject(); mdo.DoMagic("Hello"); } #region Assembly A public class BusinessObject { } public class MyBusinessObject : BusinessObject { public void DoMagic(string s) { Console.WriteLine(s); } } #endregion #region Assembly B public class MyDataObject { private delegate void DoMagicDel(BusinessObject bo, string s); private DoMagicDel

Scala: arguments contravariant and return types are covariant why? [duplicate]

时光总嘲笑我的痴心妄想 提交于 2019-12-11 16:33:32
问题 This question already has answers here : Isn't the argument type co- not contra-variant? (4 answers) Closed 5 years ago . In the FP in Scala course, Martin mentions, the arguments are "contravariant" while the return types are "covariant". I don't think I understood that completely - can someone help with this one? 回答1: Assume Bonobo extends Animal and you have a function foo of type Animal => Bonobo . In some other place you have a variable bar of type Bonobo => Animal . Should you be

Confusion with collections of nested generics

为君一笑 提交于 2019-12-11 01:05:43
问题 Please help me understand why add1() and add4() report errors and why add2() and add3() don't. Specifically, please show examples of undesired consequences if the compiler allowed each of these to compile. class InnerTypeConfusion { interface Animal {} class Dog implements Animal {} class Room<T> { void add(T t) {} } void add1(Room<? extends Animal> room) { // Error: The method add(capture#1-of ? extends Animal) in the type // Room<capture#1-of ? extends Animal> is not applicable for the //