Questions tagged [generic-variance]

Generic variance is the ability to assign a generic interface or delegate type to the same type with another parameter, for example, assign IEnumerable to IEnumerable. Generic variance has to be defined on the generic parameter type that supports them. There are two kinds of generic variance, covariance and contravariance.

58 questions
1
vote
1 answer

Why do I have to cast to type parameter and can't use the constrained type?

Can anyone explain why I have to cast to T and why Add2 does not accept Bar as a parameter? class Foo where T : class, IBar { void Add1(IDo @do) { @do.Stuff(new Bar() as T); } // Add2 does not compile: // Argument type Bar is not…
mrt181
  • 5,080
  • 8
  • 66
  • 86
1
vote
1 answer

Casting List(of List(of MyType)) to IEnumerable(of IEnumerable(of MyType))

I have a method that accepts an argument of type IEnumerable(Of IEnumerable(Of MyType)) If I do the following: Dim list1 as new List(Of MyType) From { obj1, obj2 } Dim list2 as new List(Of MyType) From { obj3, obj4 } MyMethod({ list1, list2 }) it…
Teejay
  • 7,210
  • 10
  • 45
  • 76
0
votes
0 answers

why the star projection is required in an is check in kotlin

I have this code class Dog>(private val name: Str, private val weight: T): Comparable> { override fun compareTo(other: Dog): Int { return weight.compareTo(other.weight) } override fun equals(other: Any?): Boolean { …
ntos
  • 189
  • 10
0
votes
1 answer

Usage of Type parameterization, variance vs Inheritance base class

I have a question on when to use type parameterization vs base class type when defining a method/class in Scala especially when the types allowed are in same hierarchy - constrained using type bound. for eg: trait TypeA case class SubTypeA1(in:…
Aandal
  • 51
  • 2
  • 11
0
votes
1 answer

Versatile solution for adding object of a type to an object of fitting generic type

I feel like interface (contra?)variance is the answer, but cannot find the right solution. Let us have these classes: public abstract class Fruit { } public class Banana : Fruit { } public class Apple : Fruit { } public abstract class Picture {…
Demo
  • 394
  • 1
  • 4
  • 16
0
votes
1 answer

How can I implement a method that accepts a Consumer> that is contravariant in T?

In the following sample, I can pass a Consumer to foo, but not a Consumer>. On the other hand, I can pass either type to foo2, but then I can't call the accept method of the consumer from the method body. Is…
Aaron
  • 594
  • 2
  • 12
0
votes
0 answers

Variance (covariance and contravariance) in Scala functions

I am diving in FP concepts using Scala. I think that I've finally understood what covariance and contravariance are and why function types are contravariant in their argument types and covariant in their return types. But there is still somethig…
vicaba
  • 2,836
  • 1
  • 27
  • 45
0
votes
1 answer

How to write a method that takes a parameter with a covariant or contravariant bound in scala?

I am writing a scala program, which at some point should provide some status update for some task, and it could provide it also for groups of tasks. The point is that in different phases, the details are different. So there are in fact various…
Mahdi
  • 1,778
  • 1
  • 21
  • 35
0
votes
2 answers

Is there a difference in scala type bound notation direction?

Is there a difference in scala type bound notation direction, as in is [B <: A] the same as [A >: B]?
Caballero
  • 11,546
  • 22
  • 103
  • 163
0
votes
1 answer

Accessing the generic parameter of generic parameter?

I'm having a hard time with a problem in C#. In the project I'm working on, there are some consumable classes which store information and there are consumer classes which uses those consumable classes. I've replicated the thing in a simpler way like…
Fatih BAKIR
  • 4,569
  • 1
  • 21
  • 27
0
votes
0 answers

Reason for certain restrictions on variance conversions in C#

I have a few questions about the way implicit conversions between method delegates with regards to covariance and contravariance are implemented in C#. delegate void ImplicitFunction(T thing); delegate void ExplicitFunction(T…
0
votes
0 answers

Runtime cast involving generic type parameter

I'm building a command dispatch function, such that given an object deriving from a base class, it resolves (from an IoC container) a handler for the command type and sends the command object there for processing. My types look like this: public…
carlpett
  • 12,203
  • 5
  • 48
  • 82
-1
votes
1 answer

Is this an example of a functional subtype relationship?

From this video by Functional Programming Principles in Scala "4.5 Variance" you can see a slide that says, Translated: Say you have two function types: type A = IntSet => NonEmpty type B = NonEmtpy => IntSet According to the Liskov Substitution…
Evan Carroll
  • 78,363
  • 46
  • 261
  • 468
1 2 3
4