kotlin is smart enough such that a null check will...
# random
d
kotlin is smart enough such that a null check will narrow down the type down-scope
Copy code
val thing: Thing? = Thing(name = "foo")

if (thing == null) return
// can now access thing.name here instead of thing?.name
is there a way to hint that such a "refinement" is possible in other scenarios?
Copy code
sealed class Thing { 
  abstract fun isFoo(): Boolean

  class Foo: Thing() {
    val fooValue = "hello"
    override isFoo() = true
  }

  class Bar: Thing() {
    val barValue = "goodbye"
    override isFoo() = false
  }
}

// Example

val thing: Thing = Thing.Foo()

if (thing.isFoo()) return thing.fooValue
return thing.barValue