Questions tagged [covariant]

Type constructors in programming languages which preserve subtype relations

This tag regards a feature of type-constructors in programming languages: syntactic constructs which are applied to types to produce other types. Example of type constructors: An array of elements of the original type, a pointer to the original type etc.

A unary type constructor tc is covariant if applying tc preserves the subtype relationship of types. That is, if B is a subtype of A, then tc applied to B is a subtype of tc applied to A.

If the subtype relationship is inverted rather than preserved, the type constructor is .

Further reading (Wikipedia).

72 questions
0
votes
1 answer

Override Generic Functions in Scala With "method xxx overrides nothing" Error

I am learning Scala language features. I declare a class with a type parameter. class Pair[+T](val first: T, val second: T){ // T is a covariant type. So an invariance R is introduced. def replaceFirst[R >: T](newFirst: R) = { new…
jiexray
  • 325
  • 4
  • 13
0
votes
1 answer

c# .net 4.0 Covariant vs Contravariant

I'm trying to get something working and struggling with the below when using Contravariance. My understanding is Covariance is where you can return a derived type from a base type. Contravariance is where you can pass in a derived type from a base…
tones
  • 3
  • 3
0
votes
1 answer

C++ containers, covariance and template

I have some issues to find a relevant solution to my problem. I have to return some data from a class, and the type of data kind of depends on the class. My first solution was this : class Base { virtual QVector getData() const =…
B.Oudot
  • 112
  • 1
  • 1
  • 9
0
votes
3 answers

C# Factory for generic inheritance

I apologize for this rather fundamental question, however, I could find no documentation. Perhaps because I do not know the proper terminology. Class structure: class D{} abstract class A{} class B : A {} class C : B {} I attempt to…
Nex
  • 421
  • 1
  • 4
  • 17
0
votes
2 answers

java - Overriding with covariant types wrt variables

class G { int x = 5; } class H extends G { int x = 6; } public class CovariantTest { public G getObject() { System.out.println("g"); return new G(); } public static void main(String[] args) { …
rpg
  • 1
  • 3
0
votes
1 answer

C++ covariant returning type application

I wanted to ask about covariant return type and a possible (not) application. I thought I had discovered some new design pattern, but sadly it does not work :( Let's start with an example: // test.h class B {public: virtual B* getSelf() {return…
user3770392
  • 453
  • 5
  • 12
0
votes
2 answers

Interfaces, generics, and covariant return types

Suppose I have an interface as follows: public interface Foo { T doSomething(); } Now, are both the following allowed? public class Bar implements Foo { ... } public class Bar2 extends Bar implements Foo { ... } On one…
Kelvin Chung
  • 1,327
  • 1
  • 11
  • 23
0
votes
1 answer

Virtual base function with template return type: compiler fails at derived class with pointertype as template argument (MSVC 2013)

If I derive from CBaseInterface (see code below) with template Argument T=int*, the compiler fails wirh error C2555. This happens with all pointer types used for T. If I use a typedef instead, the same code works fine. // If _FALIS is defined, the…
Anton F.
  • 466
  • 4
  • 8
0
votes
1 answer

Scala anonymous function genric variance issues

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…
Miles
  • 1
  • 1
0
votes
1 answer

Liskov Substitution Principle and Arrays invariance

The Liskov Substitution Principle tells us that if A is a subtype of B than everything we can do with type B we should be able to do with type A. So to investigate this further, I create the following: class Animal class Dog extends Animal class…
More Than Five
  • 9,959
  • 21
  • 77
  • 127
-1
votes
1 answer

Covariance with colinear vectors

I'm trying to calculate the covariance of a matrix which has two colinear vectors. I have read that it was impossible with the "cov" function from R. Does a different function exist on R to calculate the covariance of a matrix which has two colinear…
willo
  • 1
  • 1
-2
votes
1 answer

Java. Method Overriding and narrowing return type

Ok so i'm new to java, so this might be a silly question. In my superclass there is a method that takes in a generic Object and returns an Object. In my subclass I am trying to override/narrow the method to return an abstract class I've defined…
Kat
  • 21
  • 3
1 2 3 4
5