Reputation: 2118
I'm trying to figure out how to refer to this
inside of a with
block.
inline fun A.foo(bar: B, crossinline block: B.() -> Unit) {
with (bar) {
[email protected]() // compile error, "unresolved reference @A"
block()
}
}
Here is my workaround:
inline fun A.foo(bar: B, crossinline block: B.() -> Unit) {
val self = this
with (bar) {
self.doSomething() // okay, but ugly
block()
}
}
Is there a better way, and why doesn't this work?
Upvotes: 3
Views: 708
Reputation: 126
you can use this@foo to access A,or use let instead
inline fun Activity.foo(bar: Fragment) {
bar.let {
this.xxx
}
}
Upvotes: 3