这感觉有点不一致,因为它使用的是独立函数,而不是类/接口方法:
class TestClass {
val foo = "bar"
}
fun testScope(scope: TestClass.() -> Any) {
val test = TestClass()
scope(test)
}
// this works
fun TestClass.standaloneRunInTest() {
println(foo)
}
interface Example {
val name: String
// but this doesn't?
fun TestClass.interfaceRunInTest() {
println(foo + name)
}
object Instance: Example {
override val name: String = "Baz"
}
}
fun main() {
testScope {
standaloneRunInTest() // prints "bar"
Example.Instance.interfaceRunInTest() // expected to print "barbaz", but gets Unresolved reference: interfaceRunInTest
}
}
不过,更有可能的是我在做一些愚蠢的事情!