mirror of
https://github.com/jlengrand/kotlin.git
synced 2026-05-08 15:53:19 +00:00
Similar to enum entry initialization, when we have a companion object in an interface, its constructor (or clinit) initializes its state before the instance field in corresponding interface is initialized. So, interface companion object must be accessed via a captured object reference (#0, or #0.this$0 for inner anonymous objects).
37 lines
653 B
Kotlin
Vendored
37 lines
653 B
Kotlin
Vendored
// WITH_RUNTIME
|
|
|
|
import kotlin.test.*
|
|
|
|
interface Test {
|
|
companion object {
|
|
val x = "OK"
|
|
|
|
val y1 = Test.x
|
|
|
|
val y2 = 42.let { x }
|
|
|
|
val y3: String
|
|
init {
|
|
fun localFun() = x
|
|
y3 = localFun()
|
|
}
|
|
|
|
fun method() = x
|
|
val y4 = method()
|
|
|
|
val anonObject = object {
|
|
override fun toString() = x
|
|
}
|
|
val y5 = anonObject.toString()
|
|
|
|
init {
|
|
assertEquals(x, y1)
|
|
assertEquals(x, y2)
|
|
assertEquals(x, y3)
|
|
assertEquals(x, y4)
|
|
assertEquals(x, y5)
|
|
}
|
|
}
|
|
}
|
|
|
|
fun box() = Test.x |