Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to define a DSL marker annotation.
Kotlin
import kotlin.annotation.AnnotationRetention.RUNTIME import kotlin.annotation.AnnotationTarget.CLASS @DslMarker annotation class [1]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using @DslMarker as the annotation name instead of defining a custom annotation.
Forgetting to add @DslMarker before the annotation class.
✗ Incorrect
The DSL marker annotation must be defined with @DslMarker and a custom name like MyDsl.
2fill in blank
mediumComplete the code to apply the DSL marker annotation to a class.
Kotlin
@[1] class HtmlBuilder { fun body(init: BodyBuilder.() -> Unit) {} }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using @DslMarker instead of the custom annotation name.
Not annotating the class at all.
✗ Incorrect
You apply your custom DSL marker annotation (MyDsl) to the DSL builder classes to control scope.
3fill in blank
hardFix the error in the code by completing the blank to prevent implicit receiver conflicts.
Kotlin
@MyDsl class BodyBuilder { fun div(init: DivBuilder.() -> Unit) {} fun text(value: String) { println([1]) } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'this.value' which may cause confusion with receiver scope.
Using 'init' which is a lambda, not the string value.
✗ Incorrect
Inside the function, use the parameter 'value' directly to print the text content.
4fill in blank
hardFill both blanks to define two DSL builder classes with the DSL marker annotation.
Kotlin
@[1] class DivBuilder { fun span(init: SpanBuilder.() -> Unit) {} } @[2] class SpanBuilder {}
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different annotations for each class.
Using @DslMarker instead of the custom annotation.
✗ Incorrect
Both classes should be annotated with the custom DSL marker annotation to control scope properly.
5fill in blank
hardFill all three blanks to create a DSL builder function with proper scope control.
Kotlin
fun html(init: [1].() -> Unit) { val builder = [2]() builder.[3]() }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different types for receiver and instance.
Not calling the lambda parameter.
✗ Incorrect
The function takes an HtmlBuilder receiver lambda, creates an HtmlBuilder instance, and calls init on it.