covariance

Some questions about covariance in Scala

江枫思渺然 提交于 2019-12-05 05:37:02
I'm trying to understand covariance in Scala, but I cant find any examples that help me with this problem. I' ve got this code: class GenericCellImm[+T] (val x: T) {} and it compile well, but when I use it class GenericCellMut[+T] (var x: T) { } it doesn't compile. Why I can't use var (but I can use val) when I'm writing this code? How can I fix it? Also here is similar situation abstract class Sequence[+A] { def append(x: Sequence[A]): Sequence[A]} What is the problem? You can't write def append(x: Sequence[A]): Sequence[A]} , as A is in contravariant position in the append argument, while

Defining a function that calculates the covariance-matrix of a correlation-matrix

流过昼夜 提交于 2019-12-05 02:44:44
问题 I have some problems with the transformation of a matrix and the names of the rows and columns. My problem is as follows: As input-matrix I have a (symmetric) correlation matrix like this one: The correlation-vector is given by the values of the lower triangular matrix: Now, I want to compute the variance-covariance-matrix of the these correlations, which are approximately normally distributed with the variance-covariance-matrix : The variances can be approximated by -> N is the sample size

covariant type T occurs in invariant position

筅森魡賤 提交于 2019-12-05 02:19:13
I'm moving my first steps in Scala and I would like to make the following code works: trait Gene[+T] { val gene: Array[T] } The error that the compiler gives is: covariant type T occurs in invariant position in type => Array[T] of value gene I know I could do something like: trait Gene[+T] { def gene[U >: T]: Array[U] } but this doesn't solve the problem because I need a value: pratically what I'm trying to say is "I don't care of the inside type, I know that genes will have a gene field that return its content". (the +T here is because I wanna do something like type Genome = Array[Gene[Any]]

Covariance in Generics: Creating a Generic List with a Bounded Wildcard

此生再无相见时 提交于 2019-12-05 02:14:04
问题 I am looking the whole day for a proper solution, bit I am fairly new to C#. If I am right, want something similar to the Java code ArrayList<IAnimalStuff<? extends Animal>> ianimals = new ArrayList<>(); just for C#. Or another solution when I am on the wrong way. Detailed Scenario: I have a base class (Animal) and multiple subclasses (e.g. Dog). class Animal { } class Dog : Animal { } I create a common list of all animals, which contains objects of all kinds of different animals. List<Animal

Covariance and Contravariance in C#

青春壹個敷衍的年華 提交于 2019-12-05 01:14:49
I will start by saying that I am Java developer learning to program in C#. As such I do comparisons of what I know with what I am learning. I have been playing with C# generics for a few hours now, and I have been able to reproduce the same things I know in Java in C#, with the exception of a couple of examples using covariance and contravariance. The book I am reading is not very good in the subject. I will certainly seek more info on the web, but while I do that, perhaps you can help me find a C# implementation for the following Java code. An example is worth a thousand words, and I was

Covariance and Contravariance - Just different mechanisms for invoking guaranteed base class behavior?

末鹿安然 提交于 2019-12-04 21:21:00
问题 I'm having a struggle understanding these two concepts. But I think after many videos and SO QA's, I have it distilled down to its simplest form: Covariant - Assumes a sub-type can do what its base-type does. Contravariant - Assumes you can treat a sub-type the same way you would treat its base-type. Supposing these three classes: class Animal { void Live(Animal animal) { //born! } void Die(Animal animal) { //dead! } } class Cat : Animal { } class Dog : Animal { } Covariant Any animal can do

How to implement template class covariance in C++?

六月ゝ 毕业季﹏ 提交于 2019-12-04 16:48:17
Is it possible to implement a class template in such a way that one object could be casted to another if their template arguments are related? Here is an exaple to show the idea (of course it will not compile): struct Base {}; struct Derived : Base {}; template <typename T> class Foo { virtual ~Foo() {} virtual T* some_function() = 0; }; Foo<Derived>* derived = ...; Foo<Base>* base = derived; The additional problem here is that Foo is an abstract class used as an interface containing functions returning T& and T*, so I can't implement a template copy constructor. I'm writing a universal

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

匆匆过客 提交于 2019-12-04 16:08:46
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 that is a supertype of A. What problem does this solve? I'm trying to understand this on an intuitive

Is there a way to cast generic lists to lists of interface/base class types?

こ雲淡風輕ζ 提交于 2019-12-04 14:56:51
I'm trying to show someone a use for interfaces in a crazy situation they've created. They have several unrelated objects in lists, and need to perform an operation on two string properties in each object. I'm pointing out that if they define the properties as part of an interface, they can use the interface object as the type for a method parameter that acts on it; for example: void PrintProperties(IEnumerable<ISpecialProperties> list) { foreach (var item in list) { Console.WriteLine("{0} {1}", item.Prop1, item.Prop2); } } This seems like it's all good, but the lists that need to be worked on

Covariance vs. contravariance with respect to class inheritance

偶尔善良 提交于 2019-12-04 12:28:32
问题 What is the meaning of the concepts 'covariance' and 'contravariance'? Given 2 classes, Animal and Elephant (which inherits from Animal ), my understanding is that you would get a run-time errors if you try and put an Elephant into an array of Animals, and this happens because Elephant is "bigger" (more specific) than Animal. But could you place an Animal into an array of Elephant, seeing how Elephant is guaranteed to contain the Animal properties? 回答1: You have it backwards. You can add an