liveBook · Manning

@DslMarker

As explained in Kotlin in Action, 2E

@DslMarker is a Kotlin meta-annotation for restricting which receivers of the same DSL marker are available implicitly in nested scopes.

@DslMarker restricts implicit receivers in nested Kotlin DSL blocks. Without it, outer receivers remain available and can make an expression invoke the wrong receiver. section 13.2.2

Marking an HTML builder DSL

@DslMarker annotation class HtmlTagMarker @HtmlTagMarker open class Tag With this marker, nested receivers such as A and IMG cannot both be used implicitly when their types share the marker. An ambiguous assignment like href = "https://..." therefore fails; use an explicit receiver when necessary.

How Kotlin in Action, 2E covers it

How deep it goes
Defines the meta-annotation and shared-marker receiver rule, then demonstrates the resulting compile-time error.
Examples
Uses nested table, row, cell, anchor, and image builders, including an invalid href assignment.
What you can do after
Apply it via Kotlin annotation syntax to make nested DSL receiver usage safer and require explicit receivers when ambiguity remains.
How it connects
Connects lambdas with receivers, implicit receivers, DSL builders, annotation classes, and explicit receiver syntax.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Kotlin in Action, 2E. The section links point to the parts of the book it draws on.