Generic variance is the ability to assign a generic interface or delegate type to the same type with another parameter, for example, assign IEnumerable
Questions tagged [generic-variance]
58 questions
2
votes
1 answer
Class type parameters in Scala.
I'm having difficulty writing something that should be quite straight-forward, but I can't seem to get the syntax right.
I have a class hierarchy for foods:
Food :> Vegetable :> Bamboo
And for animals:
Animal :> Herbivore :> Panda
And I'm trying…

ehrt1974
- 1,166
- 2
- 11
- 24
2
votes
1 answer
Creating covariant generic types without violating empty interface rules
Background: I wanted to "expand" the .NET Lazy<> type to support implicit cast between Lazy and the underlying T object to be able automatically unwrap the containing value. I was able to do so fairly easily:
public class ExtendedLazy :…

Arian Motamedi
- 7,123
- 10
- 42
- 82
2
votes
2 answers
Scala - returning same type as passed as argument
Let's say I have a class M that classes A,B,C inherit:
abstract M
A extends M
B extends M
C extends M
And i should like to do something like this:
val a0:A = ...
val b0:B = ...
val c0:C = ...
val a1:A = transform[A](a0)
val b1:B =…

User1291
- 7,664
- 8
- 51
- 108
2
votes
1 answer
C# Delegate under the hood question
I was doing some digging around into delegate variance after reading the following question in SO : Delegate.CreateDelegate() and generics: Error binding to target method
I found a very nice bit of code from Barry kelly at…

Ted
- 345
- 5
- 18
2
votes
2 answers
Class hierarchy problem (with generic's variance!)
The problem:
class StatesChain : IState, IHasStateList {
private TasksChain tasks = new TasksChain();
...
public IList States {
get { return _taskChain.Tasks; }
}
IList IHasTasksCollection.Tasks {
…

devoured elysium
- 101,373
- 131
- 340
- 557
1
vote
0 answers
Java Covariant Override of Method with Contravariant Consumer as a Parameter
In the following code example the compiler does not allow me to override the method f.
public class A {}
public class B extends A {}
public class X {
public void f(Consumer super A> consumer) {
consumer.accept(new A());
…

Stefan Dollase
- 4,530
- 3
- 27
- 51
1
vote
1 answer
How to work around the lack of invariant/contravariant type parameters in Dart?
Dart unfortunately lacks (by design) the ability to specify invariant or contravariant type parameters. So when I need them, how do I work around their absence?
Take the abstract setting of a Producer and a Consumer class.
abstract class…

Anakhand
- 2,838
- 1
- 22
- 50
1
vote
0 answers
Scala variance positions - theory behind it?
Scala has notion of "variance position" and fancy rules around it, especially when variance is combined with method type bounds. Rules ensure type safety, one can read them in Scala lang spec, or see briefed in "Programming in Scala" 5ed:
To…

Max
- 1,741
- 3
- 23
- 40
1
vote
3 answers
generics JAVA in c++? how to do ?
class T : public std::string {
public:
T(char* s) : std::string(s){};
};
class X : public T {
public:
X(char* s) : T(s) {};
~X() {};
};
template T doIt(const T arg);
int main(int argc, const char*…

thiagoh
- 7,098
- 8
- 51
- 77
1
vote
1 answer
Workaround for TypeScript not inferring contravaraince?
TypeScript doesn't seem to infer contravariance. Here is an example that illustrates the inconsistency:
class Base { base = "I'm base" }
class Der extends Base { der = "I'm der" }
interface Getter { get(): E }
interface Setter { set(value:…

Juan
- 15,274
- 23
- 105
- 187
1
vote
0 answers
Implicit resolution fails for a contravariant type with a type bound
The following code compiles:
class X[U, T <: U]
object X {
implicit def genericX[U, T <: U]: X[U, T] = new X[U, T]
}
implicitly[X[String, String]] // compiles
When we make T covariant (class X[U, +T <: U]) it compile as well:
class X[U, +T <:…

Kamil Kloch
- 333
- 1
- 9
1
vote
1 answer
How can I handle this "which is not a subtype of overridden" error
I'm trying to write some validatable form interface in Kotlin. In the validation part I'm using https://github.com/kamedon/Validation.
Here is the very simple code I'm trying to run;
import com.kamedon.validation.Validation
abstract class…

Onur Eren Elibol
- 251
- 1
- 2
- 12
1
vote
1 answer
Another (simpler) trouble with type variance
I had a (looong) version of this problem posted yesterday here:
Trouble with type variance
To cut the (really) long story short, this:
class A[-P, T <: P]
does not compile (it complains, that "P occurs in covariant position in type <: P of type…

Dima
- 39,570
- 6
- 44
- 70
1
vote
1 answer
Kotlin: Confusion with lambdas and generics
Please see error messages in comments:
interface Printable {}
class Book(val title: String) :Printable
fun bookPrint(b: Book?):String = "Title: " + b?.title
class Author(val name: String) :Printable
fun authorPrint(a: Author?):String = "Name: "…

GlenPeterson
- 4,866
- 5
- 41
- 49
1
vote
1 answer
Problem using Lazy from within a generic abstract class
I have a generic class that all my DAO classes derive from, which is defined below. I also have a base class for all my entities, but that is not generic.
The method GetIdOrSave is going to be a different type than how I defined SabaAbstractDAO, as…

James Black
- 41,583
- 10
- 86
- 166