3

if I want to check a nullable Boolean I get a type mismatch

var bool: Boolean? = true

if(bool) 
  println("foo") 
else 
  println("bar")

because Boolean is expected not Boolean?

karlsebal
  • 1,449
  • 17
  • 23
  • Related to (duplicate of?) https://stackoverflow.com/questions/58974510/how-to-check-if-two-boolean-values-are-true-in-kotlin – gidds Dec 06 '19 at 19:16
  • Well, related maybe – but not so clear and easy to understand imho – karlsebal Dec 06 '19 at 19:52
  • Yes, but that question had _my_ answer, which was of course _incredibly_ clear and easy to understand :-) – gidds Dec 06 '19 at 20:36
  • True! But the example code was not :D – karlsebal Dec 06 '19 at 20:39
  • Does this answer your question? [Use of Boolean? in if expression](https://stackoverflow.com/questions/32830904/use-of-boolean-in-if-expression) – Ilya Dec 07 '19 at 02:12

3 Answers3

5

If you want to treat null case differently from either true or false:

when(bool) {
    null -> println("null")
    true -> println("foo")
    false -> println("bar")
}
Alexey Romanov
  • 167,066
  • 35
  • 309
  • 487
3

use Boolean.equals()

var bool: Boolean? = null

if(true.equals(bool)) 
  println("foo") 
else 
  println("bar")

it is even possible to do this inline

var bool: Boolean? = null

if(true == bool) 
  println("foo") 
else 
  println("bar")

Or use elvis nullable boolean check

var bool: Boolean? = null

if(bool ?: false) 
  println("foo") 
else 
  println("bar")
karlsebal
  • 1,449
  • 17
  • 23
  • The reason for this is that a nullable Boolean? has three states (true/false/ null) and not just two (true/false), so you have to check against true or false explicitly. – lionthefox Dec 06 '19 at 14:06
  • 3
    No good reason to use `equals` here, `==` calls `equals` but treats `null` correctly so you can safely write `bool == true`. I'd still go with the last option because it makes the intention clear and doesn't _look_ like an antipattern like `== true` does. – Alexey Romanov Dec 06 '19 at 15:43
  • The 2nd example here is the one I think Intellij IDEA Kotlin plugin recommends as idiomatic. – Jayson Minard Dec 06 '19 at 15:48
  • 1
    IMO, the second example is far easier to reason about than the third, although I'd flip it around to `bool == true`. `== true` is an immediate indicator we're dealing with a nullable since it's idiomatic. It would only make me pause if I knew the author was a beginner programmer. – Tenfour04 Dec 06 '19 at 16:45
  • Yes, apparently so (for `bool == true` over `bool ?: false`). The motivation is https://github.com/yole/kotlin-style-guide/issues/18#issuecomment-225417634 and there's also some useful discussion in https://discuss.kotlinlang.org/t/is-the-idiom-for-checking-nullable-booleans-helpful/8238 – Alexey Romanov Dec 06 '19 at 17:57
-1

When you have nullable type my suggestion is this:

If you put nullable for a reason it means that variable could be nullable, so before do anything you have to check if it's not null.

 val bool: Boolean? = null

     if(bool != null) {
         if(bool){
             println("foo")
         } else {
             println("foo")
         }
    } else {
    println("variable non instancied yet")
   }
Dak28
  • 259
  • 1
  • 8