If I used a variable to check for boolean, the "!isNumber" is highlighted with the warning "Condition ‘!isNumber’ is always true":
val isNumber = bind.number.isChecked
when (array) {
"A" -> {
if (isNumber) {
return "number"
} else if (!isNumber) {
return "letter"
}
}
However if I used the view directly to check for boolean, there is no warning:
when (acArray) {
"A" -> {
if (bind.number.isChecked) {
return "number"
} else if (!bind.number.isChecked) {
return "letter"
}
}
>Solution :
Your if is already checking for the true value of isNumber, so you don’t need to explicitly check isNumber in else block if its false, because the opposite of true is false that’s why you get that warning.
Imagine the compiler talking to you:
Compiler:
Don’t tell my
elseto check if itsfalsebecause myifblock
is already checking if itstrue, let myifdo the heavy lifting and leave myelseto just wake up whenisNumberbecomesfalse, no need to remind my poorelse, leave him be…