Skip to content

Instantly share code, notes, and snippets.

@rvprasad
Last active April 21, 2019 04:11
Show Gist options
  • Save rvprasad/d6cabc815313051d3055e28fa75c251f to your computer and use it in GitHub Desktop.
Save rvprasad/d6cabc815313051d3055e28fa75c251f to your computer and use it in GitHub Desktop.
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)
p1.read()
var p2: Producer<Double> = Producer<Number>(3) // Disallowed
p2.read()
var c1: Consumer<Number> = Consumer<Double>() // Disallowed
c1.write(3)
var c2: Consumer<Double> = Consumer<Number>()
c2.write(0.4)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment