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

TypeScript: Hacking around unsoundness in a Map `getOrCreate` helper function

I have a helper function for getting an entry from a map, adding it if it's not present already. export function mapGetOrCreate(map: Map, key: K, valueFn: (key: K) => V): V { let value = map.get(key); if (value === undefined) { …
Kannan Goundan
  • 4,962
  • 3
  • 24
  • 31
4
votes
2 answers

Kotlin: Returning Array from function with return type Array if E is enum class that implements interface I

Recently I ran into a problem where I had a function which had to return an Array of Is, in form of all values of enum E, with E implementing interface I, with every code that came to my mind compiler complained about type mismatch: Error:(x, x)…
William Stanley
  • 693
  • 1
  • 7
  • 22
4
votes
1 answer

Kotlin: Making subclasses of generic types inherit functions and restricting the available types of output

I have the following Kotlin classes implementing something similar to the Collections interface or a set that contains multiple elements. abstract class MyCollection> { abstract fun contains(e: Member):…
user1661303
  • 539
  • 1
  • 7
  • 20
3
votes
3 answers

How to instantiate a class as the interface that it derives from with constrained generic type parameter

There's the following interface which defines a packet. public interface IPacket { int Size { get; } } There are two implementations, each with its own additional property. public class FooPacket : IPacket { public int Size => 10; …
Moss
  • 855
  • 1
  • 9
  • 23
3
votes
1 answer

Kotlin generics: counterintuitive type inference and checking with out keyword

I've been recently learning Kotlin, while having some questions with covariant type. The sample code is here. I have Option and Option2 both having a type parameter T and a run extension. I could understand the first two run in validation(), since…
austin.s
  • 178
  • 6
3
votes
2 answers

Use-Site variance in Kotlin

open class A class B: A() fun copy(src: MutableList, dst: MutableList) { for (i in 0 until src.size) { dst.add(i, src[i]) } } For the above mentioned code I understand that copy function expects both type parameters of…
mallaudin
  • 4,744
  • 3
  • 36
  • 68
3
votes
2 answers

How to implement a Kotlin interface that refers to the conforming type?

In the interest of silly thought experiments whose primary purpose is to explore how part of a language works, I decided I wanted to explore a method of making Python programmers more comfortable in Kotlin. Simplistically, I can do this by…
Travis Griggs
  • 21,522
  • 19
  • 91
  • 167
3
votes
1 answer

Scala covariance and lower bound i dont get it

I am currently learning scala and i am confused about the variance annotations especially the covariance and contravariance. So i did some research an came upon following example class Box[+T] { def put[U >: T](x: U): List[U] = { …
Jay
  • 1,035
  • 2
  • 11
  • 22
3
votes
3 answers

Generic typing so that one method's result can be used as other method's parameter again

I have code that boils down to a Factory initializing an object and then using that object again to do additional operations: trait Factory[T] { def initialize(): T; def finish(t: T): Unit; } As I understand this, the result of initialize…
Silly Freak
  • 4,061
  • 1
  • 36
  • 58
3
votes
2 answers

Variance of function types wrt. interfaces

I'm trying to understand the variance rules for function types. It seems they don't treat input and output the same (up to duality). Consider this program. let mk1 s = s |> Seq.iter (fun _ -> ()) // val mk1 : s:seq<'a> -> unit let mk2 = mk1 :…
Søren Debois
  • 5,598
  • 26
  • 48
2
votes
0 answers

mypy does not recognize derived generic with different variance than the base class

When type checking the following code block with mypy an error is raised. from typing import Generic, TypeVar class Employee: pass class Manager(Employee): pass T_co = TypeVar('T_co', Employee, Manager, covariant=True) T_contra =…
2
votes
1 answer

Is my workaround for covariant generic parameters necessary?

I started off with a simple generic interface: interface IFooContext { TObject Value { get; } String DoSomething( Expression> lambdaExpression ); } // Usage: IFooContext ctx = ... String str =…
Dai
  • 141,631
  • 28
  • 261
  • 374
2
votes
2 answers

How does contravariance work with Func delegate in .net core

I have the following piece of code where I am trying to write a generic validation rule for my domain objects. while doing so I have an issue to deal Func delegate supporting variance public class Person { } public class Employee : Person {…
user824910
  • 1,067
  • 6
  • 16
  • 38
2
votes
2 answers

Variance of Scala List map function

I have a question that's been bugging me. Lists in Scala are covariant (List[+A]) Let's say we have these classes: class A class B extends A The map function of List[B] takes a function f: B => C But I can also use a f: A => C which is a subclass…
2
votes
1 answer

Force type parameter to be invariant at use-site when it is covariant at declaration site

I am building an extension function on KProperty1. The function needs to accept an argument that extends the value type of property (R), even though KProperty1 is covariant in the type parameter R. A slightly contrived example would be the…
Yona Appletree
  • 8,801
  • 6
  • 35
  • 52