I’ve got two classes from dependencies, let’s call them both Demo
, in package com.example.a
and package com.example.b
.
I want to create two extension functions in the same file to convert between the two Demo
classes.
I need to reference the outer this
in a scoped function.
I tried the following two solutions, but neither compiles:
1.
import com.example.a.Demo as DemoA
import com.example.b.Demo as DemoB
func DemoA.toDemoB(): DemoB =
foo {
name = [email protected]
}
func DemoB.toDemoA(): DemoA =
foo {
name = [email protected]
}
func com.example.a.Demo.toDemoB(): com.example.a.Demo =
foo {
name = [email protected]
}
func com.example.a.Demo.toDemoA(): com.example.a.Demo =
foo {
name = [email protected]
}
Here’s the best answer I could come up with. It’s not ideal as it’s less concise than option 1, but at least it compiles.
import com.example.a.Demo as DemoA
import com.example.b.Demo as DemoB
func DemoA.toDemoB(): DemoB {
val demoA = this
return foo {
name = demoA.name
}
}
func DemoB.toDemoA(): DemoA {
val demoB = this
return foo {
name = demoB.name
}
}