What is a "receiver" in Kotlin?

How is it related to extension functions? Why is with a function, not a keyword?

There appears to be no explicit documentation for this topic, only the assumption of knowledge in reference to extensions.

What is an orgasm?, An orgasm is a feeling of intense pleasure that happens during sexual activity. It's sometimes called coming or climaxing. Most people can have orgasms. What� A or a is the first letter and the first vowel letter of the modern English alphabet and the ISO basic Latin alphabet. Its name in English is a (pronounced / ˈ eɪ /), plural aes.

Function Literals/Lambda with Receiver

Kotlin supports the concept of "function literals with receivers". It enables the access on visible methods and properties of a receiver of a lambda in its body without any additional qualifiers. This is very similar to extension functions in which it’s also possible to access visible members of the receiver object inside the extension.

A simple example, also one of the greatest functions in the Kotlin standard library, isapply:

public inline fun <T> T.apply(block: T.() -> Unit): T { block(); return this }

As you can see, such a function literal with receiver is taken as the argument block here. This block is simply executed and the receiver (which is an instance of T) is returned. In action this looks as follows:

val foo: Bar = Bar().apply {
    color = RED
    text = "Foo"
}

We instantiate an object of Bar and call apply on it. The instance of Bar becomes the "receiver". The block, passed as an argument in {}(lambda expression) does not need to use additional qualifiers to access and modify the shown visible properties color and text.

The concept of lambdas with receiver is also the most important feature for writing DSLs with Kotlin.

What is a personal health budget?, A personal health budget is an amount of money to support your health and wellbeing needs, which is planned and agreed between you (or someone who� Alcoholics Anonymous is an international fellowship of men and women who have had a drinking problem. It is nonprofessional, self-supporting, multiracial, apolitical, and available almost everywhere.

What is a coronavirus? - Elizabeth Cox, Dig into the science of coronaviruses and find out how they cause diseases such as SARS Duration: 5:16 Posted: 14 May 2020 Names such as Karen, Becky and Chad have been co-opted to call out social faux pas online. Here’s where they came from — and what the people named before the meme think about their online

Simply put ( without any extra words or complications) , the "Receiver" is the type being extended in the extension function or the class name. Using the examples given in answers above

 fun Foo.functionInFoo(): Unit = TODO()

Type "Foo" is the "Receiver"

 var greet: String.() -> Unit = { println("Hello $this") }

Type "String" is the "Receiver"

Additional tip: Look out for the Class before the fullstop(.) in the "fun" (function) declaration

fun receiver_class.function_name() {
   //...
}

What is a verb?, Verbs are used to describe an action, state or occurrence. Find out how you can use them in your writing with this Bitesize Primary English guide. UTF-8 Encoding Debugging Chart. Here is a Encoding Problem Chart that aids in debugging common UTF-8 character encoding problems. See these 3 typical problem scenarios that the chart can help with.

What Is a Mammogram?, A mammogram is an X-ray picture of the breast. Doctors use a mammogram to look for early signs of breast cancer. Regular mammograms are the best tests� YouTube is a free video-sharing website and the second largest search engine behind Google Search.; All you need is a Google account to create a YouTube account so you can start watching videos or creating your own content.

Coronavirus (COVID-19) frequently asked questions, What are you looking for? Enter a word or phrase below to locate questions and answers that match. � Go. If you see a primary care physician for your general healthcare , there’s a chance you’re seeing a D.O., not an M.D. While both degrees mean your doctor is a licensed physician, their training differs slightly and each has a unique perspective on care. As Brian Krachman, D.O. , an internal medicine

What is a pandemic?, A pandemic is the worldwide spread of a new disease. An influenza pandemic occurs when a new influenza virus emerges and spreads� If you're wondering, "What is a Chromebook?" here's all you need to know about what powers the device, its ecosystem, and if it's the right choice for you.

Comments
  • Existing answers have already told what a receiver is, but it may be helpful to understand where the word comes from: in message-passing object-oriented languages like Smalltalk, method calls are conceived of as messages passed to an object. The object on which the method is called is the "receiver" of the message.
  • Question: I read (Foo).() -> Unit as a function that takes a Foo as a receiver and no parameter. If that's true, how come you're invoking it with an argument Foo()?
  • @AbhijitSarkar Function types with a receiver have their parameter list prefixed with the receiver. This should be in the main body of the post, editing in..
  • You can also have multiple receivers when you define extension inside class
  • @Panel You raise a valid point, but if somebody comes for this post to understand what a receiver is, the static/virtual dispatch differences might be a too high bar...heck, even trips me up sometimes.
  • Here is details explanation of different kind of receivers. blog.kotlin-academy.com/…
  • In this case we can make different type of calls: greet("my text") which has the same effect with "my text".greet()
  • I don't get it. greet is defined as a method that has a String receiver but no parameters. So I understand how we can call "Fitzgerald".greet(), but how can we call greet("Fitzgerald")?
  • (FYI the link to the Simon Wirtz article is broken.)