This code doesn’t compile
interface SmallerThing
class Thing: SmallerThing
interface Repo<T, ID> {
fun findById(id: ID): T
}
class ThingRepo: Repo<Thing, Long> {
override fun findById(id: Long) = Thing()
}
class LazyEntity<T, ID>(val id: ID) {
fun load(repo: Repo<out T, ID>): T = repo.findById(id)
}
interface Foo {
val lazyThing: LazyEntity<out SmallerThing, Long>
}
class Bar(override val lazyThing: LazyEntity<Thing, Long>) : Foo
fun main() {
val bar: Foo = Bar(LazyEntity(42))
val repo: ThingRepo = ThingRepo()
println(bar.lazyThing.load(repo))
}
with the error message:
cenasxumjVC.kt:27:32: error: type mismatch: inferred type is ThingRepo but Repo<Nothing, Long> was expected
println(bar.lazyThing.load(repo))
^
I don’t understand why this happens, ThingRepo
should be a subtype of Repo<out SmallerThing, Long>
and load should accept it but it complains about wanting Nothing
in the first param