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
2
votes
1 answer

Why scala List could take covariant type as paramenter in method +=

Scala List is declared as sealed abstract class List[+A] extends AbstractSeq[A] with LinearSeq[A] with Product with GenericTraversableTemplate[A, List] with LinearSeqOptimized[A, List[A]] with java.io.Serializable The method to prepend an…
Chen
  • 58
  • 5
2
votes
0 answers

C++ covariant-return error while passing self-class name as a template parameter

I got this error: prog.cpp: In instantiation of 'class Tree >': prog.cpp:13:16: error: invalid covariant return type for 'Self* Tree::branch() [with LeafT = int; Self = C]' Self * branch() {} …
Valentin T.
  • 519
  • 3
  • 12
2
votes
7 answers

What exactly are covariant return types in C++?

I get a compile error when I try to do this: class A { virtual std::vector test() { /* do something */ }; } class B: public A { virtual std::vector test() { /* do something */ }; } I assume that A and B are covariant types, and…
user3697176
  • 211
  • 3
  • 13
2
votes
3 answers

Covariant return type with non-pointer/reference return type

I'm trying to implement a .NET framework like collection class in C++(11). My problem is an invalid covariant type. I have these classes: template class IEnumerator { public: virtual bool MoveNext() = 0; …
Francesco Boffa
  • 1,402
  • 1
  • 19
  • 35
1
vote
3 answers

Covariance and Contravariance in Scala

I have a confusion in understanding covariance type being restricted in method parameters. I read through many materials and I am unable to get them the below concept. class SomeThing[+T] { def method(a:T) = {...} <-- produces error } In the…
user3103957
  • 636
  • 4
  • 16
1
vote
1 answer

Covariant data types: Why return type must be same as or child of its 'parent method' return type?

As this is not so famous concept, I will make a little intro. Covariant return type of a method is one that can be replaced by a "narrower" type when the method is overridden in a subclass. So I can compile just fine this little program (as String…
Stefan
  • 969
  • 6
  • 9
1
vote
4 answers

c++ inheritance and container covariance

I am trying to build the following architecture: struct A{ int x; } struct B: public A{ int additinal_data; } struct ContainerA{ std::vector va; } struct ContianerB{ std::vector vb; int additional_data; } I want to reorganize the…
Solon
  • 362
  • 1
  • 13
1
vote
1 answer

plot survival curve after adjusting for gender

I have a dataset that have samples with / without treatment and their ages at death and gender. All the samples are dead. I want to test if the treatment affects the survival. The dataset df looks like below FID gender age_at_death treatment…
tianbu
  • 11
  • 1
1
vote
0 answers

Covariant(out) & generic type kotlin

In Java I can do this: interface BaseInterface {} interface ExtendsInterface extends BaseInterface {} public interface ProblemInterface { void method(E baseInterface); } class Main { …
1
vote
1 answer

Why not set covariant as default when define subtype in Scala?

It means define trait Option[T] is same as trait Option[+T]. It's easy to consider val humanOpt: Option[Human] can point to a Option[Student] instance just like val humanOpt: Human can point to a Student instance. Maybe it seems some strange but…
LoranceChen
  • 2,453
  • 2
  • 22
  • 48
1
vote
2 answers

C# Generic Covariant Error

following is my code, i don't know why DateTime can not change to Object , any idea to resolve this problem? public class Test { public DateTime CreatedTime { get; set; } } public class Test1 { } public…
Clover
  • 266
  • 1
  • 4
  • 14
1
vote
1 answer

Explanation on the error with for comprehension and co-variance

Question Would like to get assistance to understand the cause of the error. The original is from Coursera Scala Design Functional Random Generators. Task With the factories for random int and random boolean, trying to implement a random tree…
mon
  • 18,789
  • 22
  • 112
  • 205
1
vote
1 answer

Why don't generic types work with inheritance in Scala?

So here's the code: package week4 object expr { abstract class Expr[T] { def eval:T = this match { case Number(x) => x case Sum(e1, e2) => e1.eval + e2.eval } def show: String = this match { case Number(x) => "" +…
Himanshu Mishra
  • 310
  • 3
  • 13
1
vote
1 answer

How to interpret :: in scala

I write the following code: class Animal class Bird extends Animal val animalList = List(new Animal, new Animal) new Bird :: animalList The implementation of the :: method looks like this: def ::[B >: A] (x: B): List[B] = new…
cstur4
  • 966
  • 2
  • 8
  • 21
1
vote
4 answers

What’s the overhead of await without I/O?

One downside of the async pattern in C# 5 is that Tasks are not covariant, i.e., there isn't any ITask. I have noticed that my developers often do return await SomeAsyncMethod(); to come around this. Exactly what impact…
Anders
  • 17,306
  • 10
  • 76
  • 144