I'm trying to understand why can't a variable that is assigned from a class field change in a function. Consider this example:
class A {
private var a: String? = "a"
fun hello() {
val b = a
if (b != null) {
// The compiler assumes that 'b' is always not null inside this branch
b.length
} else {
// is null
}
}
}
Variable b is going to always be the same throughout the function (later is implicit non null in the if branch). So even if through concurrency I change the field a, the variable b does not change?
How is this possible? does Kotlin make a copy of the variable and assigns it to b? I thought assignments worked with references and such.
This can happen, as far as I'm concerned, with any type (String, Int, custom class, etc)