0
0
Kotlinprogramming~10 mins

Why DSLs improve readability in Kotlin - Test Your Understanding

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a simple DSL function that prints a greeting.

Kotlin
fun greet() {
    println([1])
}

greet()
Drag options to blanks, or click blank then click option'
Aprintln("Hello")
BHello, DSL!
C"Hello, DSL!"
Dgreet
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to put quotes around the greeting text.
Passing a function name instead of a string.
2fill in blank
medium

Complete the code to create a DSL builder function that adds two numbers.

Kotlin
fun addNumbers(block: () -> Int): Int {
    return [1]()
}

val result = addNumbers { 5 + 3 }
println(result)
Drag options to blanks, or click blank then click option'
AaddNumbers
Bblock
Cprintln
Dresult
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to return the function name without calling it.
Using println instead of calling the block.
3fill in blank
hard

Fix the error in the DSL function that builds a greeting message.

Kotlin
fun greeting(block: GreetingBuilder.() -> Unit): String {
    val builder = GreetingBuilder()
    builder.[1](block)
    return builder.message
}

class GreetingBuilder {
    var message = ""
    fun sayHello() {
        message = "Hello from DSL!"
    }
}

println(greeting { sayHello() })
Drag options to blanks, or click blank then click option'
Aapply
Brun
Cinvoke
Dcall
Attempts:
3 left
💡 Hint
Common Mistakes
Using run which returns the block result instead of the object.
Trying to call invoke or call which are not applicable here.
4fill in blank
hard

Fill both blanks to create a DSL that builds a list of strings with a custom builder.

Kotlin
class StringListBuilder {
    val items = mutableListOf<String>()
    fun add(item: String) { items.[1](item) }
}

fun buildList(block: StringListBuilder.() -> Unit): List<String> {
    val builder = StringListBuilder()
    builder.[2](block)
    return builder.items
}

val list = buildList {
    add("apple")
    add("banana")
}
println(list)
Drag options to blanks, or click blank then click option'
Aadd
Bapply
Cremove
Drun
Attempts:
3 left
💡 Hint
Common Mistakes
Using remove instead of add to add items.
Using run instead of apply which returns the object.
5fill in blank
hard

Fill all three blanks to create a DSL that builds a map with keys and values.

Kotlin
class MapBuilder {
    val map = mutableMapOf<String, Int>()
    fun put(key: String, value: Int) { map.[1](key, value) }
}

fun buildMap(block: MapBuilder.() -> Unit): Map<String, Int> {
    val builder = MapBuilder()
    builder.[2](block)
    return builder.[3]
}

val result = buildMap {
    put("one", 1)
    put("two", 2)
}
println(result)
Drag options to blanks, or click blank then click option'
Aput
Bapply
Cmap
Drun
Attempts:
3 left
💡 Hint
Common Mistakes
Using run instead of apply which returns the builder.
Returning the builder instead of the map property.
Using add instead of put for the map.