Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of Boolean? in if expression

Tags:

null

kotlin

People also ask

DO IF statements use Boolean logic?

The if statement has the following syntax: if ( condition ) statement; if is a Java reserved word The condition must be a boolean expression. It must evaluate to either true or false.

Is if a boolean operator?

Examples of Boolean operators in the IF statementThe IF statement will be successful if the comparisons of the first three expressions evaluate to TRUE, or if expressions four or five evaluate to TRUE.

Can you use == with Booleans?

Boolean values are values that evaluate to either true or false , and are represented by the boolean data type. Boolean expressions are very similar to mathematical expressions, but instead of using mathematical operators such as "+" or "-", you use comparative or boolean operators such as "==" or "!".

Can we use boolean in if statement?

Here is a simple if-statement... The simplest if-statement has two parts – a boolean "test" within parentheses ( ) followed by "body" block of statements within curly braces { }. The test can be any expression that evaluates to a boolean value – true or false – value (boolean expressions are detailed below).


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 
}

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-> {}
}

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)
}

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
  }
}

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 */
}

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 */
}