Implicit Receivers

Unlike the let() and also() methods, the run() and apply() methods executed their lambdas in the context of their own target. What’s cool about Kotlin is that the language and library implementors didn’t keep that as a privileged execution for themselves. Instead, they made it accessible very elegantly to every one using the language. If you’re eager to learn and execute such methods for your own code, the wait is over. And this technique is one of the most essential to create fluent syntax for DSLs, so it’s all the more exciting.

Passing a Receiver

In JavaScript, functions may take zero or more parameters, but you may also pass a context object—a receiver—using either the call() method or apply() function. Kotlin’s ability ...

Get Programming Kotlin now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.