What is a "receiver" in Kotlin and why is it needed?
Hello colleagues! I just started learning Kotlin after Java and came across the term "receiver". I'm reading the documentation and see things like `apply`, `let`, `run`, `with` — and everywhere they write about receivers. But I can't fully understand what it is and how it works.\n\nHere's a simple example. I write this code:\n\n```kotlin\ndata class Person(var name: String = "", var age: Int = 0)\n\nfun main() {\n val person = Person().apply {\n name = "Alice" // Here I access name directly, without person.name\n age = 30\n }\n println(person)\n}\n```\n\nI understand that inside the `apply` block I can write `name = "Alice"` instead of `person.name = "Alice"`. But how does it work? What does "function with receiver" mean? Is it some kind of magic closure? Or is it similar to how
No answers yet.
Log in to leave an answer