Files
kotlin-fork/compiler/testData/codegen/box/classes/kt2224.kt
T
Juan Chen d163853c97 [FIR] add support for implementation by delgation
This commit handles "subclass: super-interface by delegate-expression".

During Psi2Fir, for each delegate, we add to the subclass a synthetic
field (which has type super-interface), and an assignment of the
delegate-expression to the synthetic field in the primary constructor,
so that the delegate-expression can be resolved and transformed along
the way.

During Fir2Ir, we look up delegatable members from the super-interface
and generate corresponding functions/properties for the subclass.

TODO: support for generic delegatable members and generic
super-interface.
2020-07-08 09:42:24 +03:00

45 lines
754 B
Kotlin
Vendored

interface A {
fun foo(): Int
}
class B1 : A {
override fun foo() = 10
}
class B2(val z: Int) : A {
override fun foo() = z
}
fun f1(b: B1): Int {
val o = object : A by b { }
return o.foo()
}
fun f2(b: B2): Int {
val o = object : A by B2(b.z) { }
return o.foo()
}
fun f3(b: B2, mult: Int): Int {
val o = object : A by B2(mult * b.z) { }
return o.foo()
}
fun f4(b: B1, x: Int, y: Int, z: Int): Int {
val o = object : A by b {
fun bar() = x + y + z
}
return o.foo()
}
fun box(): String {
if (f1(B1()) != 10) return "fail #1"
if (f2(B2(239)) != 239) return "fail #2"
if (f3(B2(239), 2) != 239*2) return "fail #3"
if (f4(B1(), 1, 2, 3) != 10) return "fail #4"
return "OK"
}