danneu
07/09/2017, 6:52 PMval 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?
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