In Kotlin 1.9, the type system became stricter abo...
# compiler
h
> In Kotlin 1.9, the type system became stricter about nullability with generics. When you use Optional.of() or Optional.emptyE(), they require a non-null type. Without the : Any bound, E could be nullable (e.g., String?), causing the E & Any vs E mismatch errors. I am working to upgrade my company to Kotlin 1.9. Do you agree with this explanation? I'm seeing failures left and right on functions with Optional and AI is telling me to migrate them from
<E>
to
<E : Any>
.
d
Comparing to what version? In the 1.9 by itself were not much of changes around type inference, as the team was focused on stabilizing the K2 compiler for the 2.0 release. I found only the following changes regarding nullability inference: KT-46727, KTLC-58.
h
Good point, comparing to 1.8
This particular code base has been using this pattern since Kotlin 1.3, this worked fine but is now a problem in 1.9
d
Most likely it's KTLC-58.
h
> • Report TYPE_MISMATCH errors since 1.9, >
-XXLanguage:-ProhibitUsingNullableTypeParameterAgainstNotNullAnnotated
can be used to temporarily revert to the pre-1.8 behavior So I need to either bite the bullet now or I can delay it until 2.0 using that flag?
d
I would recommend perform migrations as soon as possible. Otherwise they tend to accumulate and increase the amount of work for the next version update. And 1.9 -> 2.0 needs quite a lot of migrations (comparing to other version updates).
h
Yeah, makes sense. I was trying to find some quick hack I could do centrally, but clear, thank you Dmitriy!
👌 1