Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"this@" syntax for extension function

Tags:

kotlin

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?

like image 720
EntangledLoops Avatar asked Dec 19 '25 02:12

EntangledLoops


1 Answers

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
    }
}
like image 89
Silence Avatar answered Dec 21 '25 16:12

Silence



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!