Pablichjenkov
06/24/2025, 3:28 AM1.8.22 to 1.9.25 and I am noticing some changes I haven’t heard of. I have the following Java definition:
public abstract class Abc<T> {
void function1(@NonNull T t)
}
In Kotlin 1.8.22, below code compiles without issues.
class AbcSubclass1<T> : Abc<T> {
override fun function1(T t) = Unit
}
But in Kotlin 1.9.25, I get a type mismatch error. Then the compiler/IDE suggest the following code which compiles perfectly:
class AbcSubclass2<T> : Abc<T> {
override fun function1(T & Any t) = Unit
}
Notice the type T vs T & Any, it seems like a new language enhancement to typing.
Am I right?
Is this the best fix or Perhaps defining the class like below is better?
class AbcSubclass2<T : Any> : Abc<T> ...Szymon Jeziorski
06/24/2025, 6:24 AMclass AbcSubclass2<T : Any> : Abc<T> as you said.
I would only go for per-method T & Any in case when Java class you're inheriting/implementing contained both methods that could work with nullable T and the ones that expect not-nullable TPablichjenkov
06/24/2025, 1:27 PMYoussef Shoaib [MOD]
06/24/2025, 7:47 PMAny, that'd be allowed. I think this should thus compile. Maybe file a bug on youtrack?Pablichjenkov
06/24/2025, 7:52 PMPablichjenkov
06/24/2025, 7:54 PMYoussef Shoaib [MOD]
06/25/2025, 3:08 AMoverride works in Kotlin lol. So yeah you have to match the signature in every way except the return type, which can be a subtype. Hence this error is actually consistent
I.e this compiles:
abstract class Base<T> {
abstract fun foo(): Any
}
class Bar<T>: Base<T>() {
override fun foo(): String = "hi"
}
but this doesn't:
abstract class Base<T> {
abstract fun foo(x: String)
}
class Bar<T>: Base<T>() {
override fun foo(x: Any) {}
}
and similarly, your case shouldn't compile either. A better error message would be nice potentially thoughPablichjenkov
06/25/2025, 3:37 AM