What is a "receiver"?

Yes.

Yes and no. For for functions like let and run this is right. You can construct a function which takes a lambda with both it and this.

fun foo(code: String.(Int) -> Unit){
    "test".code(5)
}

fun main(){
   foo {
       println(this)
       println(it)
   }
}

Here the code lambda has a reciever (of type String) and a single parameter (of type Int). So you can mix this and it but I can’t remember any real world example where this is done.


So the reciever is the part in an extension function before the function name. It pretends to be the class the function belongs to. It can either be accessed with this but same as for normal methods you can omitt the this keyword for properties like this.foo or function calls like this.bar().

It is another special case. Normaly when you create a lambda you need to name the parameters of that lambda

someList.filter { entry -> entry !=  foo }

However if a lambda only has a single parameter it will have a default name it.

someList.filter { it != foo }

If you want to you can still give it any name you like with the above syntax.

1 Like