155

Programmer dictionary: Receiver – Kotlin Academy

 6 years ago
source link: https://blog.kotlin-academy.com/programmer-dictionary-receiver-b085b1620890?gi=64cafbc36850
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Programmer dictionary: Receiver

1*DEd6rW4cgOnqAr3yDFDBeg.png

Terms “receiver” and “sender” comes from sending letter metaphor. We can think about the sender as the object which invokes a method (postman delivering an email), the receiver is the object whose method is invoked (person receiving the letter). Let’s look at a simple example:

class Car {
private val engine = Engine()fun startEngine() {
engine.start()
}
}

In above example, an instance of Engine class is receiver and instance of Car class is the sender.

Under the hood when an object receives a message, it determines what method is being requested and passes control to the method

In Kotlin we mainly talk about receiver when discussing Lambdas with receiver and Extensions. The receiver is a general term, but there are also a few more specific terms related to receivers:

Any block of code in Kotlin may have one or more receivers. It makes functions and properties of the receiver available in that block of code without qualifying it.

References

Sender/Receiver object


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK