Oh, that's very interesting. To my knowledge, this is the only place in the Kotlin language where you can assign a variable to itself?
If you specify types explicitly,
return object {
val x: String = x
}
you get the error
Variable 'x' must be initialized
, so the compiler does notice that this is unsafe.
@Jonathan Olsson to avoid such surprises in the future, if you put your cursor on one variable, IntelliJ will highlight all other usages of the
same variable (and not other variables that have the same name), so it becomes easy to notice that this is trying to assign
x
to itself and not using the
x
from
A
.