Lukasz Kalnik
07/15/2022, 8:42 AMif
?
val selectedScene1: String? = // ...
val selectedScene2: String? = // ...
if (selectedScene1 != null || selectedScene2 != null) {
val sceneToFind: String = selectedScene1 ?: selectedScene2!! // The !! are necessary to make this work
}
Michael de Kaste
07/15/2022, 8:47 AMlistOfNotNull(selectedScene1, selectedScene2).firstOrNull() //on null, you're not in your 'if'. On value, you are.
Lukasz Kalnik
07/15/2022, 8:49 AM?:
cannot be resolved at compile time if I understand this correctly.sceneToFind
should always be non-nullable inside the if
(even without the !!
). But I suppose the nullability is resolved by type checker which doesn't follow the logic of the program but just statically compares resolved types.ephemient
07/15/2022, 9:53 AMif (selectedScene1 != null || selectedScene2 != null)
and replace it with
val sceneToFind: String? = selectedScene1 ?: selectedScene2
if (sceneToFind != null) {
then you will get effectively the same result, but safelyLukasz Kalnik
07/15/2022, 9:59 AM