我从依赖项中获得了两个类,让我们将它们都称为Demo
,位于包com.example.a
和包com.example.b
中.
我想在同一个文件中创建两个扩展函数,以在两个Demo
类之间进行转换.
我需要在作用域函数中引用外部this
.
我try 了以下两种解决方案,但都没有编译:
1.
import com.example.a.Demo as DemoA
import com.example.b.Demo as DemoB
fun DemoA.toDemoB(): DemoB =
foo {
name = this@DemoA.name
}
fun DemoB.toDemoA(): DemoA =
foo {
name = this@DemoB.name
}
fun com.example.a.Demo.toDemoB(): com.example.a.Demo =
foo {
name = this@com.example.a.Demo.name
}
fun com.example.a.Demo.toDemoA(): com.example.a.Demo =
foo {
name = this@com.example.a.Demo.name
}