I have observed a weird logical change in the following code while migrating from kotlin 1.9.x to 2.x.x
Copy code
fun main() {
val vr = SnippetVR(object: Interaction {
override fun print() {
println("hello")
}
})
val snippet = vr.create()
snippet.print()
println("bye")
}
interface Interaction {
fun print()
}
class SnippetVR(val interaction: Interaction? = null) {
fun create(): Snippet {
return Snippet.newInstance().apply { this?.interaction = interaction }!!
}
}
class Snippet() {
companion object {
fun newInstance(): Snippet? = Snippet()
}
var interaction: Interaction? = null
fun print() {
interaction?.print()
}
}
On k1.9, the code outputs 'hello' as well as 'bye'
But on k2, 'hello' is not printed, only 'bye' is printed.
In k2, the interaction is being self assigned and not being picked from class variable.