Challenge - 5 Problems
Infix DSL Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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()) }
Attempts:
2 left
💡 Hint
Remember that the build function joins items with a comma and space.
✗ Incorrect
The infix function 'add' adds items to the list. The build function joins them with a comma and space, so the output is 'apple, banana'.
❓ Predict Output
intermediate2: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()) }
Attempts:
2 left
💡 Hint
Check how the words are joined in the build function.
✗ Incorrect
Each 'add' call adds a word and returns the builder, allowing chaining. The build joins words with spaces, so output is 'Hello world !'.
🔧 Debug
advanced2: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" }
Attempts:
2 left
💡 Hint
Check if 'to' is defined or imported in this context.
✗ Incorrect
The code tries to use 'to' as an infix function or operator, but it is not defined or imported, causing an 'Unresolved reference: to' error.
🧠 Conceptual
advanced1: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?
Attempts:
2 left
💡 Hint
Think about the return type flexibility of infix functions.
✗ Incorrect
Infix functions can return any type, not necessarily Unit. The other options are required for infix functions.
❓ Predict Output
expert3: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()) }
Attempts:
2 left
💡 Hint
Consider how the infix 'add' returns 'this' and how chaining affects the tree structure.
✗ Incorrect
Each 'add' adds a child to the current node and returns the same node, so all children are added to 'root'. Chaining means the second 'add' is called on the result of the first, which is 'root', so children are siblings. So output is option D.