Is it possible to pass `this` as a parameter to an...
# announcements
d
Is it possible to pass
this
as a parameter to an interface delegate?
Copy code
interface A

class B(private val c : C) : A

class C : A by B(this)
produces an error saying that "`this` is not defined in this context". Is there a workaround for this?
s
It is not legal unfortunately. It has to do with the fact that the synthesized code could call the constructor of B with a C that is not fully initialized.
👍 1
s
You could instead go for composition, and use by lazy. Something like val a : A by lazy { B(this) } I just decided it as a solution recently. Am I right guys?
🤔 1
d
Ok, thanks @Sam Garfinkel! @Sandesh Baliga, unfortunately I have a requirement that C must implement A. Thanks though!
👍 1
h
You could have interface A<T> { val self:T } Which would not be "this" ( because one can implement freely) but then you can implement interface and do override val self: B = this