0Pricing
Kotlin Academy · Lesson

@DslMarker: Preventing Receiver Leakage

Use @DslMarker annotations to enforce DSL nesting rules at compile time.

The Receiver Leakage Problem

In nested DSLs, an inner builder lambda can accidentally access members from an outer receiver. For example, inside a td { } block you might call tr { } from the outer table scope — which is almost never intended.

What Leakage Looks Like

Without @DslMarker, the compiler allows calling outer-scope functions from any inner lambda, which leads to confusing, error-prone DSL usage:

// Without @DslMarker — this compiles but is wrong:
html {
    body {
        table {
            tr {                // outer scope leaks in
                tr {  }         // calling tr inside tr — nonsensical
            }
        }
    }
}

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