Hi, I was having problem understanding how a variance change caused some code to not compile. Below is the snippet – Basically SelfInv and SelfOut are two recursive generic type. The former is annotated invariant while the latter is annotated as covariant. Then BoxForInv and BoxForOut are two types with type parameter constraints SelfInv and SelfOut.
The compiler error suggests that such type BoxForInv<SelfInv<*>>
can’t exist, while still allows BoxForOut<SelfOut<*>>
– this is where it confuses me.
interface SelfInv<A: SelfInv<A>>
interface SelfOut<out A: SelfOut<A>>
interface BoxForInv<A: SelfInv<A>>
interface BoxForOut<A: SelfOut<A>>
fun canCompile(): BoxForOut<SelfOut<*>>? = null
fun cannotCompile(): BoxForInv<SelfInv<*>>? = null
// ^ Type argument is not within its bounds.
// Expected: SelfInv<SelfInv<*>>
// Found: SelfInv<*>