@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
- Lambda with Receiver: The DSL Foundation
- @DslMarker: Preventing Receiver Leakage
- Building a Type-Safe HTML/Config DSL
- Testing and Evolving DSLs Without Breaking Users