尝试使用泛型类型类,但遇到以下问题,即:
类型不匹配:推断的类型是
ChildClass但SuperClass<SuperType>预期
open class SuperClass<T> where T: SuperType {
fun modifySomething(input: T): T {
input.someValue.inc()
return input
}
}
open class SuperType {
val someValue: Int = 0
}
class ChildClass : SuperClass<ChildType>()
class ChildType: SuperType() {
fun getModifiedValue(): Int {
return someValue
}
}
class TestEnvironment {
fun testType(superClass: SuperClass<SuperType>) {
// do something with superClass
}
fun itDoesntWork() {
testType(ChildClass()) // compilation error
}
}
Run Code Online (Sandbox Code Playgroud)
所需的结果是函数 testType(superClass: SuperClass<SuperType>) 应在 不使用 …