https://kotlinlang.org logo
Title
n

Norbi

02/04/2023, 8:20 PM
Could you please explain why this does not compile?
abstract class BaseClass<BaseType> {

    fun f1(p: BaseType) {}
}

sealed interface IBase<T1, T2> {

    class I1<T1>: IBase<T1, Unit>
}

class InheritedClass<T1, T2>: BaseClass<IBase<T1, T2>>() {

    fun f2() {
        // Compile error:
        // Type mismatch.
        // Required: IBase<T1, T2>
        // Found: IBase.I1<T1>
        f1(IBase.I1<T1>())
    }
}
Thanks.
r

Roukanken

02/04/2023, 8:30 PM
because
IBase.I1<T>
's supertype is
IBase<T1, Unit>
not
IBase<T1, T2>
Since compiler can't prove that
Unit
==
T2
it's a type mismatch Imagine you did this:
val test = InheritedClass<String, Int>()
test.f2()
now,
T2
is
Int
which is definitely not
Unit
as you want to assign to it
what exactly are you trying to do?
n

Norbi

02/04/2023, 8:49 PM
It is an excerpt from a fairly complex code, so I cannot easily explain - probably I over-complicated the generics part of it 🙂 Thanks for your reply.