Hi people, trying K2 and looks like `requireNotNul...
# k2-adopters
e
Hi people, trying K2 and looks like
requireNotNull
still treated as possible to return null
s
Can you please give a code example?
e
So the code is
Copy code
private val popUpModel: RelevantContent.Popup
        get() = requireNonNull(requireArguments().getParcelableCompat(PopupViewModel.ARG_KEY_POP_UP))
And the
getParcelableCompat()
looks like
Copy code
inline fun <reified T : Parcelable> Bundle.getParcelableCompat(key: String): T? {
    return if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) {
        getParcelable(key) as? T
    } else {
        getParcelable(key, T::class.java)
    }
}
That doesn't matter both cases of it return null
Compiler error
Copy code
Return type mismatch: expected 'com.company.RelevantContent.Popup', actual 'com.company.RelevantContent.Popup?'.
Interesting, that modifying code to
Copy code
private val popUpModel: RelevantContent.Popup
        get() {
            val popup = requireNonNull<RelevantContent.Popup>(requireArguments().getParcelableCompat(PopupViewModel.ARG_KEY_POP_UP))
            return popup
        }            return popup
Fixes it
This one also compiles
Copy code
private val popUpModel: RelevantContent.Popup
        get() = requireNonNull<RelevantContent.Popup>(requireArguments().getParcelableCompat(PopupViewModel.ARG_KEY_POP_UP))
But gives warning in IDE that explicit type is redundant
Let me search tracker for ticket
s
Can you please also share the definition of the
requireNonNull
, probably it is the culprit
e
Haha, thanks! That is java Objects
Copy code
public static <T> T requireNonNull(T obj) {
        if (obj == null)
            throw new NullPointerException();
        return obj;
    }
I was mixing it with kotlin
requireNotNull
Chanaged to Kotlin and error is gone!
👍 1
s
Oh! I see. Looks like the Java version should be annotated to work properly - but please report the case to the YouTrack, since it is still a behavior change
e
Oke, java version has no annotations. And it is Android implementation of JDK.
👍 1
😞 1
I will submit ticket to Google to add annotation for the new Android. Or maybe it is already fixed there. I will keep you updated.
🙏 1
s
But it was compiling fine with Kotlin 1.9?
e
Finally, I've created the ticket
s
Thank you very much! Can you please add a small code example if possible?
e
Sure