Skip to content

Instantly share code, notes, and snippets.

Last active April 21, 2019 04:11
What would you like to do?
Understanding Contravariance and Covariance via Kotlin
class Producer<out T: Any>(val e:T) {
fun read(): T = e
class Consumer<in T: Any>() {
private lateinit var e: T
fun write(v: T): Unit { e = v }
fun main() {
var p1: Producer<Number> = Producer<Double>(0.4)
var p2: Producer<Double> = Producer<Number>(3) // Disallowed
var c1: Consumer<Number> = Consumer<Double>() // Disallowed
var c2: Consumer<Double> = Consumer<Number>()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment