covariance

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

C++ covariance in parameters

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-06 08:01:12
问题 I wanted to know why C++ does not support co-variance in parameters like in example below or if there is a way to achieve it? class base { public: virtual base* func(base * ptr) { return new base(); } }; class derived : public base { public: virtual derived* func(derived * ptr) override { return new derived(); } //not allowed }; 回答1: The return type is permissible since derived inherits from base , but the function parameter can't work - not all base instances will be a derived also. What's

more on type parameters for scala, trying to get a consistent reference to a type

送分小仙女□ 提交于 2020-01-06 07:23:51
问题 OK, so, I've asked about this before. Ideally I'm looking for a general answer that will help me understand how to specify types consistently, but in lieu of that, I'll settle for how to solve specific problems. So far each solution seems to bring 3 more issues, I'm trying to avoid putting an entire application here but my goal is to find a way to refer to the type of a recursively parameterized trait type from anywhere, in a useful way, in a non-trivial program, where values of that trait

Invalid variance: The type parameter 'T' must be invariantly valid on 'xxx.IItem<T>.GetList()'. 'T' is covariant [duplicate]

元气小坏坏 提交于 2020-01-05 04:02:11
问题 This question already has answers here : Invalid variance: The type parameter 'T' must be contravariantly valid on 'UserQuery.IItem<T>.ItemList'. 'T' is covariant [duplicate] (2 answers) Closed 5 years ago . Why the following code get the error? Invalid variance: The type parameter 'T' must be invariantly valid on 'UserQuery.IItem<T>.GetList()'. 'T' is covariant. public interface IFoo {} public interface IBar<T> where T : IFoo {} public interface IItem<out T> where T: IFoo { IEnumerable<IBar

Scala - covariant type in mutable collections

六眼飞鱼酱① 提交于 2020-01-04 07:37:07
问题 I am new in Scala world and now I am reading the book called "Scala in Action" (by Nilanjan Raychaudhuri), namely the part called "Mutable object need to be invariant" on page 97 and I don't understand the following part which is taken directly from the mentioned book. Assume ListBuffer is covariant and the following code snippet works without any compilation problem: scala> val mxs: ListBuffer[String] = ListBuffer("pants") mxs: scala.collection.mutable.ListBuffer[String] = ListBuffer(pants)

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

Casting generics and the generic type

戏子无情 提交于 2020-01-03 10:54:48
问题 Consider, I have the following 3 classes / interfaces: class MyClass<T> { } interface IMyInterface { } class Derived : IMyInterface { } And I want to be able to cast a MyClass<Derived> into a MyClass<IMyInterface> or visa-versa: MyClass<Derived> a = new MyClass<Derived>(); MyClass<IMyInterface> b = (MyClass<IMyInterface>)a; But I get compiler errors if I try: Cannot convert type 'MyClass<Derived>' to 'MyClass<IMyInterface>' I'm sure there is a very good reason why I cant do this, but I can't