Here is my code
class X<A> {
val x = extensionX()
}
fun <A> X<A>.extensionX () = 1 //compiles
fun <N, B: Box<N, B>> B.extension() = this.a
abstract class Box<N, B:Box<N,B>>(val a: N) {
abstract fun copy(new: N): B
val boxed = extension() //doesn't compile
}
class MyBox<N>(a: N) : Box<N, MyBox<N>>(a) {
fun value() = extension() //doesn't compile
override fun copy(new: N) = MyBox(new)
}
val z = MyBox(5).extension() //compiles
(I added the copy
method to demonstrate why I need this structure.)
I get two compilation errors when calling the extension
Unresolved reference. None of the following candidates is applicable because of receiver type mismatch:
public fun <N, B : Box<TypeVariable(N), TypeVariable(B)>> TypeVariable(B).extension(): TypeVariable(N) defined in ...
I can’t see why the reciever type is mismatched, and how I can write this code differently