0Pricing
Kotlin Academy · Lesson

Lambda with Receiver: The DSL Foundation

Understand function types with receivers and how they enable DSL syntax.

What Is a Lambda with Receiver?

A lambda with receiver is a function literal where the body has access to the members of a designated receiver object — without any qualifier. The type looks like: (ReceiverType) -> ReturnType, written as ReceiverType.() -> ReturnType.

Syntax Comparison

A regular lambda: { value: Int -> value + 1 }. A lambda with receiver: StringBuilder.() -> Unit — inside this lambda, this refers to the StringBuilder instance.

val addHello: StringBuilder.() -> Unit = {
    append("Hello")   // this = StringBuilder
    append(", World")
}

All lessons in this course

  1. Lambda with Receiver: The DSL Foundation
  2. @DslMarker: Preventing Receiver Leakage
  3. Building a Type-Safe HTML/Config DSL
  4. Testing and Evolving DSLs Without Breaking Users
← Back to Kotlin Academy