Receiver functions
Kotlin has built-in language features for creating custom DSLs. Receiver functions and the Infix
keyword (covered in the next section) are two of those features.
Although intended primarily for creating DSLs, receiver functions can be also useful in everyday programming and as we shall see later, the Kotlin standard library uses them in several utility functions.
We could say that receiver functions share some similarities with extension functions. They have the same syntax for marking the receiver and, inside the function, they can access members of the receiver instance. Here's how we'd define a receiver function type:
val hello: String.() -> Unit = { print("Hello $this") }
It's the same as a normal function type definition, with the addition of the receiver type and a dot before the function signature.
To call this function, we have to provide an instance of the receiver type, a string in our case:
hello("Kotlin") // prints Hello Kotlin
You can see how, inside the receiver...