I wrote a condition as below
if (subsriber?.isUnsubscribed && isDataEmpty()) {
loadData()
}
As my subscriber could be null. The above title error displayed. So I cast it as below
if (subsriber?.isUnsubscribed as Boolean && isDataEmpty()) {
loadData()
}
It looks not as nice. Is there a better way of doing this?
Kotlin Boolean Expression A Boolean expression returns either true or false value and majorly used in checking the condition with if...else expressions. A boolean expression makes use of relational operators, for example >, <, >= etc.
An idiomatic way to do it in Kotlin would be using the when expression: var b: Boolean? = null when (b) { true -> ... false -> ... else -> ... }
If the key is present, then the […] operator will return the corresponding Boolean value; but if the key is not present (which could happen if the map doesn't contain a "default" key), it returns null. However, the variable you're trying to assign it to is of type Boolean , which doesn't allow nulls.
TL;DR Kotlin has the === operator for referential integrity and none of true , false or null references the same thing. Don't forget that, when comparing nullable booleans.
A boolean type can be declared with the Boolean keyword and can only take the values true or false: Just like you have learned with other data types in the previous chapters, the example above can also be written without specifying the type, as Kotlin is smart enough to understand that the variables are Booleans:
In Kotlin, there are no primitive data types such as int or boolean in Java. Instead, we have “boxed” primitive types like Int and Boolean. Kotlin’s nullable Boolean type Boolean? is pretty similar to Java’s Boolean type.
Basically, kotlin is strong, and also it is the statically typed programming language for each and every variable that deals with the expression and it’s the type that can be used for compiled programming language.
A Boolean expression returns a Boolean value: true or false. You can use a comparison operator, such as the greater than ( >) operator to find out if an expression (or a variable) is true:
I usually resolve this situation with the ?:
operator:
if (subsriber?.isUnsubscribed ?: false && isDataEmpty()) {
loadData()
}
This way, if subscriber
is null
, subsriber?.isUnsubscribed
is also null
and subsriber?.isUnsubscribed ?: false
evaluates to false
, which is hopefully the intended result, otherwise switch to ?: true
.
Also casting a nullable type with as Boolean
is unsafe and will throw an exception if null
is encountered.
Also is you have just Required: kotlin.Boolean. Found: kotlin.Boolean? you can do this:
when(something?.isEmpty()) {
true -> { }
false -> { }
null -> { }
}
Also if you are interested only in one simple conditional statement
if(something?.isEmpty() == true){
this will only worked if not null && empty
}
i know it's answered question but for future viewers can be helpful
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With