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
41
votes
3 answers

Covariant generic parameter

I'm trying to understand this but I didn't get any appropriate results from searching. In C# 4, I can do public interface IFoo { } How is this different from public interface IFoo { } All I know is the out makes…
TheOtherGuy
36
votes
2 answers

Why does Resharper say, "Co-variant array conversion from string[] to object[] can cause run-time exception on write operation" with this code?

This code: comboBoxMonth.Items.AddRange(UsageRptConstsAndUtils.months.ToArray()); public static List months = new List { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", …
10
votes
4 answers

Differing return type for virtual functions

A virtual function's return type should be the same type that is in base class, or covariant. But why do we have this restriction?
amit
  • 101
  • 1
  • 4
10
votes
2 answers

Why doesn't Java 5+ API take advantage of covariant return types?

Since Java 5 we are allowed to have covariant return types. Why doesn't the Java API take advantage of this? Take Graphics2D.create() for instance. Why isn't it overridden to return a Graphics2D object? It seems to me that it would be backward…
aioobe
  • 413,195
  • 112
  • 811
  • 826
9
votes
6 answers

Covariant Return Type in Interface not compiling via Javac

I have the following structure: public interface BarReturn {} public interface FooReturn {} public interface FooBarReturn extends FooReturn, BarReturn {} public interface Foo { FooReturn fooBar( ); } public interface Bar { BarReturn…
9
votes
2 answers

Why in java method overriding allows to have covariant return types, but not covariant parameters?

For example I have a Processor base class with a method that returns an Object and takes Object as a parameter. I want to extend it and create a StringProcessor which will return String and take String as parameter. However covariant typing is only…
user2992672
  • 398
  • 1
  • 3
  • 10
8
votes
3 answers

What is the proper OO way to convert an object to one of its subclasses (covariant return type)?

I have a subclass that needs to return a subclass of the return type of its parent class. I believe this is called a covariant return type. I am wondering the simplest way to convert from the parent to the child class. class A { } class B extends…
aw crud
  • 8,791
  • 19
  • 71
  • 115
8
votes
3 answers

java covariant return type

Why does below code prints "1" ? class A { int x = 1; } class B extends A { int x = 2; } class Base { A getObject() { System.out.println("Base"); return new B(); } } public class CovariantReturn extends Base { B…
kris979
  • 81
  • 2
8
votes
3 answers

Invalid covariant type with CRTP clonable class

I'm trying to implement a Clonable class with the CRTP. However, I need to have abstract class that have a pure virtual clone method, overridden by child classes. To make this happen, I need the clone function to return a covariant return type. I…
Guillaume Racicot
  • 39,621
  • 9
  • 77
  • 141
8
votes
3 answers

"Invalid covariant return type" errors in nested classes with methods returning template-based objects

The following C++ code gives me these errors when compiled: covariant.cpp:32:22: error: invalid covariant return type for ‘virtual Q C::test()’ covariant.cpp:22:22: error: overriding ‘virtual Q B::test()’ I do not want to change the line…
pvh1987
  • 621
  • 5
  • 8
  • 12
6
votes
1 answer

Java covariant array bad?

I've been told by several people that Java allows covariant array subtyping in other words if A is a subtype of B, then A[] is a subtype of B[], but that this is a bad feature because it can lead to runtime errors. Can someone give me a concrete…
user98289
  • 139
  • 9
6
votes
3 answers

How covariant method overriding is implemented using bridging Technique in java

While reading on Covariant Overriding, i find out very strange fact, covariant method overriding is implemented using a bridging technique. it also said that this feature is implemented in java5 and above.(i think it is because generics…
Prateek
  • 12,014
  • 12
  • 60
  • 81
5
votes
1 answer

Are Arrays in Ada Covariant?

I know in Java arrays are covariant. So for example: Assume Dog is a subclass of Animal In java the arrays are covariant making: Animal[] a supertype of Dog[] But in java generic collections are not covariant such as: ArrayList is not a…
jbeverid
  • 291
  • 7
  • 23
5
votes
0 answers

assigning mock functions on `import *` with flowjs

Say I have the following code: export const exampleFunc = () => {} Which is then used in a test like so: import * as exampleAll from '../../path/to/example' describe('Example', () => { exampleAll.exampleFunc = jest.fn() }) This fails to…
Abraham P
  • 15,029
  • 13
  • 58
  • 126
5
votes
1 answer

Java covariant return types not working for overriding methods of an enum instance?

I spent quite a while with Google to find some information on this topic, but results relating to both Java enums and covariant return types were pretty much non-existent. So: is it possible to use covariant return types with enum methods where you…
Søren Boisen
  • 1,669
  • 22
  • 41
1
2 3 4 5