Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

What happens when using 'this' inside a lambda expression that is sent to let function?

I’ve looked at the source code of let function:

    @kotlin.internal.InlineOnly
public inline fun <T, R> T.let(block: (T) -> R): R {
    contract {
        callsInPlace(block, InvocationKind.EXACTLY_ONCE)
    }
    return block(this)
}

i clearly understand that using it keyword inside of the block (code) i’m sending to
let function will refer to the object that called the let function.
But on which object does this keyword will refer if i will use it inside the block (code).
The way i see it, it should be the same object that it refer to, but i’m not sure.
My computer gone bad so i can’t test it by myself.
Can someone not only give me the answer but also will explain me the logic behind the this keyword in the situation above?

example of code:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

val p:Preson = Person()
p.let{**this**}

>Solution :

this is just one of parameters (called a "receiver") that could be passed to the lambda by a calling code. If lambda doesn’t support a receiver, as in the let example, this will be just this of the enclosing code:

class Foo {
    fun foo() {
        val p = Person()
        p.let{ this } // `this` is `Foo`
    }
}

If lambda has a receiver then it depends what will be passed to it as this. For example, a very similar function to let, but passing the object as this is apply:

class Foo {
    fun foo() {
        val p = Person()
        p.apply{ this } // `this` is `Person`
    }
}

Of course, this doesn’t have to be the object that we invoked the function on. It could be just any object, it depends on what the function will pass to the lambda. For example, in Kotlin stdlib we have a buildString() function that instantiates a StringBuilder, runs the lambda passing the StringBuilder as this and then returns a string that was built by the lambda. We use this function like this:

val s = buildString {
    // `this` is `StringBuilder`
    append("hello") // implicit this.append()
    append("world")
}

By looking into buildString() signature we can see that the lambda receives StringBuilder as its receiver:

public inline fun buildString(builderAction: StringBuilder.() -> Unit): String {

You can read more about lambdas with receivers in the documentation: https://kotlinlang.org/docs/lambdas.html

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading