Olaf Gottschalk
02/14/2023, 4:44 PMsealed class Blah<T> {
abstract fun doSomething(): T
abstract fun doAnotherThing(a: T)
}
object A : Blah<Int>() {
override fun doSomething(): Int = 42
override fun doAnotherThing(a: Int) {}
}
object B : Blah<String>() {
override fun doSomething(): String = "42"
override fun doAnotherThing(a: String) {}
}
// inferred type is Blah<out Any>
fun gimmeBlah() = if (Random.nextBoolean()) A else B
// error is Type mismatch. Required: Blah<Any?> Found: A
fun doesNotCompile(): Blah<Any?> = if (Random.nextBoolean()) A else B
I do NOT understand, why the inferred type introduces the "out" variance.... it does not make sense to me (and does not work in my context....)Ayfri
02/14/2023, 4:46 PMBlah<*>
instead of Blah<Any?>
Olaf Gottschalk
02/14/2023, 4:48 PMOlaf Gottschalk
02/14/2023, 4:49 PM