contravariance

Variance rules in C#

不羁岁月 提交于 2020-01-12 14:23:12
问题 The Exact rules for variance validity are a bit vague and not specific. I'm going to list the rules for what makes a type valid-covariantly, and attach some queries and personal annotations to each of those rules. A type is valid covariantly if it is: 1) a pointer type, or a non-generic type. Pointers and non-generic types are not variant in C#, except for arrays and non-generic delegates. Generic classes, structs and enums are invariant. Am I right here? 2) An array type T[] where T is valid

Scala Co and Contravariance

懵懂的女人 提交于 2020-01-07 00:24:51
问题 Yes! Another of this question, and yes i already read alot of this questions in stackoverflow and still don't understand this concept and it's application. So, i'm i new comer to Scala, and like many people i still didn't get the concept of Contravariance, i'm reading the Programming Scala, 2nd Edition, and on page 283 starts the explanation of co and contravariance with the following example: gives the hierarchy: class CSuper { def msuper() = println("CSuper") } class C extends CSuper { def

Covariance/Contravariance Conundrum when using generic interface constraints

不想你离开。 提交于 2020-01-03 19:35:25
问题 public interface IShape{} public class Rectangle : IShape{} public class Base{} public class Derived : Base{} public interface IFoo<out T, in U> where T : IShape where U : Base { T Convert(U myType); } public class MyFoo : IFoo<Rectangle, Derived> { public Rectangle Convert(Derived myType) { throw new NotImplementedException(); } } class Program { static void Main(string[] args) { IFoo<IShape, Base> hmm = new MyFoo(); } } Given the above code, the compiler is unable to determine how to assign

Covariance/Contravariance Conundrum when using generic interface constraints

柔情痞子 提交于 2020-01-03 19:35:25
问题 public interface IShape{} public class Rectangle : IShape{} public class Base{} public class Derived : Base{} public interface IFoo<out T, in U> where T : IShape where U : Base { T Convert(U myType); } public class MyFoo : IFoo<Rectangle, Derived> { public Rectangle Convert(Derived myType) { throw new NotImplementedException(); } } class Program { static void Main(string[] args) { IFoo<IShape, Base> hmm = new MyFoo(); } } Given the above code, the compiler is unable to determine how to assign

Override contra-variance workaround needed

随声附和 提交于 2020-01-03 16:52:26
问题 I'm having difficulty finding the (what I'm sure is a very common) design pattern to work around the following problem. Consider this piece of code: class AA {}; class BB : public AA {}; class A { public: virtual void foo(AA& aa) = 0; }; class B : A { public: void foo(BB& bb){cout<<"B::foo"<<endl;} }; int main() { B b; BB bb; b.foo(bb); } This code will not compile because the class B does not override the pure virtual function 'foo'. The compiler considers the foo that B declares only as an

Override contra-variance workaround needed

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-03 16:52:14
问题 I'm having difficulty finding the (what I'm sure is a very common) design pattern to work around the following problem. Consider this piece of code: class AA {}; class BB : public AA {}; class A { public: virtual void foo(AA& aa) = 0; }; class B : A { public: void foo(BB& bb){cout<<"B::foo"<<endl;} }; int main() { B b; BB bb; b.foo(bb); } This code will not compile because the class B does not override the pure virtual function 'foo'. The compiler considers the foo that B declares only as an

MonoTouch and supporting variant generic interfaces

我的梦境 提交于 2020-01-01 09:53:22
问题 The below example compiles fine in regular Mono 2.10.9: namespace covarianttest { public interface ITest<out T> : IEnumerable<T> { } } However when I attempt compile it against MonoTouch 6.0.8 I receive this error: Error CS1961: The covariant type parameter 'T' must be invariantly valid on 'covarianttest.ITest' So am I to assume that MonoTouch doesn't support extending covariant/contravariant generic interfaces yet? If so what is the recommend workaround for this situation in MonoTouch? 回答1:

MonoTouch and supporting variant generic interfaces

こ雲淡風輕ζ 提交于 2020-01-01 09:53:12
问题 The below example compiles fine in regular Mono 2.10.9: namespace covarianttest { public interface ITest<out T> : IEnumerable<T> { } } However when I attempt compile it against MonoTouch 6.0.8 I receive this error: Error CS1961: The covariant type parameter 'T' must be invariantly valid on 'covarianttest.ITest' So am I to assume that MonoTouch doesn't support extending covariant/contravariant generic interfaces yet? If so what is the recommend workaround for this situation in MonoTouch? 回答1:

MonoTouch and supporting variant generic interfaces

我们两清 提交于 2020-01-01 09:53:09
问题 The below example compiles fine in regular Mono 2.10.9: namespace covarianttest { public interface ITest<out T> : IEnumerable<T> { } } However when I attempt compile it against MonoTouch 6.0.8 I receive this error: Error CS1961: The covariant type parameter 'T' must be invariantly valid on 'covarianttest.ITest' So am I to assume that MonoTouch doesn't support extending covariant/contravariant generic interfaces yet? If so what is the recommend workaround for this situation in MonoTouch? 回答1:

How to determine type parameter's variance?

我是研究僧i 提交于 2020-01-01 04:28:06
问题 Inspired by Real-world examples of co- and contravariance in Scala I thought a better question would be: When designing a library, are there a specific set of questions you should ask yourself when determining whether a type parameter should be covariant or contravariant? Or should you make everything invariant and then change as needed? 回答1: Well, simple, does it make sense? Think of Liskov substitution. Co-variance If A <: B , does it make sense to pass a C[A] where a C[B] is expected? If