How to get instance of `this` within a lambda expr...
# announcements
z
How to get instance of
this
within a lambda expression
Copy code
val sectionChildAdapter = SectionChildAdapter {
    callBackListener.invoke(it)
     this.notify(l)
}
m
Copy code
class MyClass {
    fun myFun() {
        val sectionChildAdapter = SectionChildAdapter {
            callBackListener.invoke(it)
            this@MyClass.notify(l)
        }
    }
}
but instead of doing
callBackListener.invoke(it)
, just do
callBackListener(it)
😉
z
I wanted the instance of SectionChildAdapter inside the lambda expression
m
If I’m not completely mistaken, that should just be
this
then.
similar how you just use the
callBackListener
with an implicit
this
z
Sorry for creating confusion with the way I phrased the question, problem is with the implicit this, that doesn't come inside that lambda expression
m
you could try
this@SectionChildAdapter
z
That doesn't work
m
I’d suggest switching approach from a lambda to an abstract method then - that way you could create instances like
Copy code
val adapter = object : SectionChildAdapter { 

    override fun onChildChange(item: SectionChildItem<*>) {
        callBackListener(it)
        this.notify(l)
    }
}
z
Okay
I will try that
n
wel lthen you need to define your callback like:
Copy code
private val callBackListener:  SectionChildAdapter.(SectionChildItem<*>) -> Unit
and call it like so
Copy code
adapter.callbackListener(item)
z
Woah we can even do that?
okay got that, void initially and then set listener separately