in Android Studio I wrote the following in a function:
if (startTimes.value?.containsKey(name)?:false) {
return startTimes?.value?.get(name)
}
Android Studio highlights the contents of the if
with a warning with the message Equality check should be used instead of elvis for nullable boolean check
and suggests I replace it with:
if (startTimes.value?.containsKey(name) == true) {
return startTimes?.value?.get(name)
}
Why is the second preferred by the IDE? As far as I can tell they're logically the same.
Checking nullable boolean against true
or false
is an "official" Kotlin idiom:
Nullable Boolean
val b: Boolean? = ... if (b == true) { ... } else { // `b` is false or null }
Here is the relevant discussion regarding the idiom, including opinions for and against it.