I have observed a weird logical change in the foll...
# android
a
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.
e
better ask #C7L3JB43G than here but k2 behavior looks correct according to https://kotlinlang.org/spec/overload-resolution.html#resolving-property-access
👍 2
c
And please delete this post then, cross posting is not helpful for traceability if others are looking for the answer and discussion is split up then.
e
cross-link them at least, but yes having a single copy of the question is better