Use of Boolean? in if expression
KotlinNullKotlin Problem Overview
If I have a nullable Boolean b
, I can do the following comparison in Java:
Boolean b = ...;
if (b != null && b) {
/* Do something */
} else {
/* Do something else */
}
In Kotlin, I can achieve the same by using the !!
operator:
val b: Boolean? = ...
if (b != null && b!!) {
/* Do something */
} else {
/* Do something else */
}
However, the use of !!
feels a bit sketchy to me, circumventing the null safety system.
Is there a more elegant approach for this?
Edit It seems I oversimplicated a bit. For local variables, as Banthar shows, it does work. However, my Boolean b
is actually a "property with a backing field" (I'm not really up to speed yet what this imposes). This is the result:
Kotlin Solutions
Solution 1 - Kotlin
You can compare nullable boolean with true
, false
or null
using equality operator:
var b: Boolean? = null
if (b == true) {
// b was not null and equal true
}
if (b == false) {
// b is false
}
if (b != true) {
// b is null or false
}
Solution 2 - Kotlin
If you want to cleanly check whether a Boolean?
is true
or false
you can do:
when(b) {
true -> {}
false -> {}
}
If you want to check if it's null
you can add that (or else
) as a value in the when
:
when(b) {
true -> {}
false -> {}
null -> {}
}
when(b) {
true -> {}
false -> {}
else-> {}
}
Solution 3 - Kotlin
Kotlin will statically analyze your null checks. This is fine:
val b: Boolean? = null
if (b != null && b) {
println(b)
}
Even though this fails with type error:
val b: Boolean? = null
if (b == null && b) {
println(b)
}
For more see: http://kotlinlang.org/docs/reference/null-safety.html
You can also use "null coalescing operator" (which will work for mutable variables):
val b: Boolean? = null
if (b ?: false) {
println(b)
}
Solution 4 - Kotlin
From what I've seen the Boolean? is a result of a method that returns Boolean on an object that is nullable
val person: Person? = null
....
if(person?.isHome()) { //This won't compile because the result is Boolean?
//Do something
}
The solution I've been using is to use the let
function to remove the possible returned null value like so
person?.let {
if(it.isHome()) {
//Do something
}
}
Solution 5 - Kotlin
In Kotlin, you can do like this:
val b: Boolean? = true
if (b == true) { // if b is null, this should be null == true
/* Do something */
} else {
/* Do something else */
}
Solution 6 - Kotlin
first, add the custom inline function below:
inline fun Boolean?.ifTrue(block: Boolean.() -> Unit): Boolean? {
if (this == true) {
block()
}
return this
}
inline fun Boolean?.ifFalse(block: Boolean?.() -> Unit): Boolean? {
if (null == this || !this) {
block()
}
return this
}
then you can write code like this:
val b: Boolean? = ...
b.ifTrue {
/* Do something in true case */
}
//or
b.ifFalse {
/* Do something else in false case */
}
Solution 7 - Kotlin
For Kotlin what i normally use is
if (object?.booleanProperty ==true)
{
//do stuff
}
this would only work when the property is true and the object is not null. For the reverse:
if (!object?booleanProperty !=true)
{
//do Stuff
}
Solution 8 - Kotlin
myBoolean?.let {
if(it) -> doSomething()
}
myBoolean?.let {
if(it) -> {
doSomething()
} else {
doSomethingElse()
}
}
You can also choose to do something in the event myBoolean
is null like this:
myBoolean?.let {
if(it) -> doSomething()
} ?: doSomethingInCaseOfNull()
Solution 9 - Kotlin
Let's use an if-else
statement with an Elvis Operator
:
val a: Boolean?
val b: Boolean?
a = true
b = null
if (a != null ?: b) {
println("One of them isn't nullable...")
} else {
println("Both are nullables!")
}
>// Result: "One of them isn't nullable..."
a = null
b = null
if (a != null ?: b) {
println("One of them isn't nullable...")
} else {
println("Both are nullables!")
}
>// Result: "Both are nullables!"
Solution 10 - Kotlin
It's pretty easy to add an extension function if that helps you.
fun Boolean?.orDefault(default: Boolean = false): Boolean {
if (this == null)
return default
return this
}
var x: Boolean? = null
if(x.orDefault()) {
..
}
Solution 11 - Kotlin
> In case if you want to perform operations alike contain
on a
> String
you could use equality check like below -
if (url?.contains("xxx") == true){
return false;
}
Solution 12 - Kotlin
You can do with safe Operator "let"
val b: Boolean? = null
b?.let { flag ->
if(flag){
// true Block
} else {
// false Block
}
}