0
0
Kotlinprogramming~20 mins

Infix functions in DSLs in Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Infix DSL Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of infix function call in Kotlin DSL
What is the output of this Kotlin code using an infix function in a DSL style?
Kotlin
class Builder {
    private val items = mutableListOf<String>()
    infix fun add(item: String) {
        items.add(item)
    }
    fun build() = items.joinToString(", ")
}

fun main() {
    val builder = Builder()
    builder add "apple"
    builder add "banana"
    println(builder.build())
}
A[apple, banana]
Bapple, banana
Capple banana
Dapple; banana
Attempts:
2 left
💡 Hint
Remember that the build function joins items with a comma and space.
Predict Output
intermediate
2:00remaining
Result of chained infix calls in Kotlin DSL
What is the output of this Kotlin code with chained infix function calls?
Kotlin
class SentenceBuilder {
    private val words = mutableListOf<String>()
    infix fun add(word: String): SentenceBuilder {
        words.add(word)
        return this
    }
    fun build() = words.joinToString(" ")
}

fun main() {
    val sentence = SentenceBuilder() add "Hello" add "world" add "!"
    println(sentence.build())
}
AHello world !
BHello, world, !
CHello world!
DHello world
Attempts:
2 left
💡 Hint
Check how the words are joined in the build function.
🔧 Debug
advanced
2:00remaining
Identify the error in this infix function usage
What error does this Kotlin code produce when run?
Kotlin
class Config {
    infix fun set(key: String) {
        println("Setting $key")
    }
}

fun main() {
    val config = Config()
    config set "mode" to "dark"
}
AUnresolved reference: to
BNo error, prints 'Setting mode to dark'
CSyntaxError: Unexpected token 'to'
DTypeError: 'to' is not a function
Attempts:
2 left
💡 Hint
Check if 'to' is defined or imported in this context.
🧠 Conceptual
advanced
1:30remaining
Understanding infix function constraints in Kotlin DSLs
Which of the following is NOT a requirement for a function to be used as an infix function in Kotlin?
AIt must be a member function or an extension function
BIt must have exactly one parameter
CIt must return Unit
DIt must be marked with the 'infix' keyword
Attempts:
2 left
💡 Hint
Think about the return type flexibility of infix functions.
Predict Output
expert
3:00remaining
Output of nested infix function calls in Kotlin DSL
What is the output of this Kotlin code using nested infix functions in a DSL style?
Kotlin
class Node(val name: String) {
    private val children = mutableListOf<Node>()
    infix fun add(child: Node): Node {
        children.add(child)
        return this
    }
    fun printTree(indent: String = ""): String {
        val current = "$indent$name"
        val childStrings = children.joinToString("\n") { it.printTree(indent + "  ") }
        return if (children.isEmpty()) current else "$current\n$childStrings"
    }
}

fun main() {
    val root = Node("root") add Node("child1") add Node("child2") add Node("child3")
    println(root.printTree())
}
A
root
  child1
    child2
  child3
Broot child1 child2 child3
C
root
  child1
    child2
      child3
D
root
  child1
  child2
  child3
Attempts:
2 left
💡 Hint
Consider how the infix 'add' returns 'this' and how chaining affects the tree structure.