0
0
Kotlinprogramming~10 mins

DSL scope control with @DslMarker in Kotlin - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - DSL scope control with @DslMarker
Start DSL Builder
Enter Nested DSL Scope
@DslMarker Annotation
Restrict Access to Outer Scope
Build Inner Elements
Exit Inner Scope
Continue Outer Scope
Complete DSL Build
The flow shows how @DslMarker annotation restricts access to outer DSL scopes when inside nested DSL blocks, helping avoid confusion and errors.
Execution Sample
Kotlin
import kotlin.annotation.DslMarker

@DslMarker
annotation class HtmlTagMarker

@HtmlTagMarker
class Html {
    fun body(init: Body.() -> Unit) { Body().init() }
}

@HtmlTagMarker
class Body {
    fun p(text: String) { println("<p>$text</p>") }
}

fun html(init: Html.() -> Unit) { Html().init() }

html {
    body {
        p("Hello")
        // Cannot access Html members here due to @DslMarker
    }
}
This code defines a simple HTML DSL with @DslMarker to prevent accessing outer Html scope inside Body block.
Execution Table
StepActionScopeAccessible MembersOutput/Result
1Call html { ... }Html scopeHtml.body()No output
2Enter body { ... } inside htmlBody scopeBody.p()No output
3Call p("Hello") inside bodyBody scopeBody.p()<p>Hello</p> printed
4Try to access Html members inside bodyBody scopeHtml members NOT accessible due to @DslMarkerCompilation error if tried
5Exit body blockBack to Html scopeHtml.body()No output
6Exit html blockEndNo members accessibleDSL build complete
💡 Execution stops after completing html block; @DslMarker prevents accessing outer scope inside nested blocks.
Variable Tracker
VariableStartAfter html callInside body blockAfter body blockFinal
Current ScopeNoneHtmlBodyHtmlNone
Accessible MembersNonebody()p()body()None
Key Moments - 2 Insights
Why can't I call Html's members inside the Body block?
Because @DslMarker annotation marks both Html and Body classes, Kotlin restricts access to outer scope members inside nested DSL blocks to avoid confusion, as shown in execution_table step 4.
What happens if I remove @DslMarker annotation?
Without @DslMarker, members from outer scopes like Html would be accessible inside Body, which can cause ambiguous calls or mistakes. The execution_table would show accessible Html members inside Body scope.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step is the p("Hello") function called?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Check the 'Action' column for the call to p("Hello") in the execution_table.
According to variable_tracker, what is the current scope inside the body block?
AHtml
BNone
CBody
DBoth Html and Body
💡 Hint
Look at the 'Inside body block' column for 'Current Scope' in variable_tracker.
If @DslMarker was removed, what would change in the execution_table at step 4?
AHtml members would be accessible inside Body scope
BBody members would be inaccessible
CCompilation error would occur earlier
DNo change at all
💡 Hint
Refer to key_moments explanation about scope accessibility without @DslMarker.
Concept Snapshot
DSL scope control with @DslMarker:
- Annotate DSL classes with @DslMarker
- Restricts access to outer DSL scopes inside nested blocks
- Prevents accidental calls to wrong scope members
- Helps keep DSL code clear and safe
- Use on all related DSL classes for best effect
Full Transcript
This visual execution shows how Kotlin's @DslMarker annotation controls scope in DSLs. When you enter a nested DSL block, like Body inside Html, @DslMarker prevents you from accessing members of the outer scope (Html) inside the inner scope (Body). The execution table traces each step: starting the html block, entering body, calling p("Hello"), and shows that Html members are not accessible inside Body due to @DslMarker. The variable tracker highlights how the current scope changes from Html to Body and back. Key moments clarify why this restriction exists and what happens if you remove @DslMarker. The quiz tests understanding of scope changes and annotation effects. This helps beginners see how Kotlin keeps DSLs safe and clear by controlling scope with @DslMarker.