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 to tie a receiver to a lambda expression is heavily influenced by that JavaScript capability.

Before we dive into dealing with receivers, let’s look at a regular lambda expression for a moment.

Get hands-on with 1200+ tech skills courses.