Conor Fennell
03/02/2021, 12:43 PMclass Container<in T>(private val t: T)
open class Noun
open class City: Noun()
class Capital: City()
fun goCity(container: Container<City>) {
println(container)
}
fun main() {
goCity(Container(Noun())) // pass in a super type of City, this is what I would expect as it is defined contravariance
goCity(Container(Capital())) // pass in a sub type of City, would not have expected this as it is defined contravariance
}
Marc Knaup
03/02/2021, 12:55 PMgoCity(Container(Capital()))
This creates a Container<City>
implicitly because it satisfies both:
passing in Capital
as a constructor parameter
and the expected type Container<City>
for goCity
Conor Fennell
03/02/2021, 2:24 PM