EntangledLoops
EntangledLoops

Reputation: 2118

"this@" syntax for extension function

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

Answers (1)

Silence
Silence

Reputation: 126

i try the code enter image description here

you can use this@foo to access A,or use let instead

inline fun Activity.foo(bar: Fragment) {
    bar.let {
        this.xxx
    }
}

Upvotes: 3

Related Questions