Wow, I just learned smart-casting doesn't require ...
# getting-started
c
Wow, I just learned smart-casting doesn't require a lexical scope:
Copy code
data class Foo(val a: Int)

val a: Foo? = null

val b = a
    ?.copy(a = a.a+1) // a is smart-casted here!
    ?: Foo(0)

println(b)
I thought it wasn't possible without using `let`&co. Playground link.
p
Not sure if that has to do with smart casting but the fact that if you omit the type nullability, the compiler will infer it from the statement. And in this case is clear b won't be null ever
y
Happens as well if you cast something previously in scope:
Copy code
fun Foo.foo(){
  this as Bar
  barFunction()
}