Infix functions in DSLs in Kotlin - Time & Space Complexity
We want to understand how the time it takes to run code with infix functions in Kotlin DSLs changes as the input grows.
Specifically, how does using infix functions affect the number of steps the program takes?
Analyze the time complexity of the following code snippet.
class Builder {
private val items = mutableListOf()
infix fun add(item: String) {
items.add(item)
}
fun build() = items
}
fun main() {
val builder = Builder()
builder add "apple"
builder add "banana"
builder add "cherry"
val result = builder.build()
}
This code defines a simple builder using an infix function to add items to a list, then builds the list.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Adding items to a list using the infix
addfunction. - How many times: Once per item added (here 3 times, but can be n times).
Each time we add an item, the program does one add operation.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 add operations |
| 100 | 100 add operations |
| 1000 | 1000 add operations |
Pattern observation: The number of operations grows directly with the number of items added.
Time Complexity: O(n)
This means the time to add items grows in a straight line as you add more items.
[X] Wrong: "Using infix functions makes the code run faster or slower by itself."
[OK] Correct: Infix functions only change how code looks, not how many steps it takes. The time depends on what the function does, not the infix style.
Understanding how syntax choices like infix functions affect performance helps you explain your code clearly and confidently in real projects and interviews.
What if the add function also checked for duplicates before adding? How would the time complexity change?
Practice
infix functions in Kotlin DSLs?Solution
Step 1: Understand the purpose of infix functions
Infix functions let you call a function with one argument without using dots or parentheses, making code look cleaner.Step 2: Identify the benefit in DSLs
This style makes domain-specific languages (DSLs) easier to read and write, resembling natural language.Final Answer:
They allow calling functions without dots or parentheses, making code more readable. -> Option AQuick Check:
Infix functions improve readability = C [OK]
- Thinking infix improves performance
- Believing infix allows multiple return types
- Assuming infix auto-generates UI
Solution
Step 1: Recall infix function declaration rules
In Kotlin, the keywordinfixmust appear beforefunwhen declaring an infix function.Step 2: Match the correct syntax
infix fun add(x: Int) { } correctly placesinfixbeforefunand declares the function with one parameter.Final Answer:
infix fun add(x: Int) { } -> Option AQuick Check:
Keyword order: infix before fun = A [OK]
- Placing 'infix' after function name
- Putting 'infix' after parameter list
- Omitting 'infix' keyword entirely
class Builder {
infix fun add(value: String) {
println("Added: $value")
}
}
fun main() {
val b = Builder()
b add "Kotlin"
}Solution
Step 1: Understand infix function call
The infix functionaddis called on objectbwith argument "Kotlin" without dots or parentheses.Step 2: Check the function behavior
The function prints "Added: Kotlin" to the console.Final Answer:
Added: Kotlin -> Option DQuick Check:
Infix call prints 'Added: Kotlin' = A [OK]
- Expecting syntax error due to missing parentheses
- Thinking it prints the call expression
- Assuming no output without explicit print
class Config {
infix fun set(key: String, value: String) {
println("$key = $value")
}
}
fun main() {
val c = Config()
c set ("color", "blue")
}Solution
Step 1: Check infix function parameter count
Kotlin infix functions must have exactly one parameter, but heresethas two parameters.Step 2: Understand the error cause
This causes a compilation error because infix functions cannot have multiple parameters.Final Answer:
Infix functions cannot have more than one parameter. -> Option BQuick Check:
One parameter only for infix = D [OK]
- Trying to use multiple parameters in infix
- Assuming parentheses are forbidden in all calls
- Thinking 'set' is a reserved infix name
class HtmlTag(val name: String) {
private val children = mutableListOf<HtmlTag>()
infix fun add(child: HtmlTag) {
children.add(child)
}
fun render(): String {
val inner = children.joinToString("") { it.render() }
return "<$name>$inner</$name>"
}
}
fun main() {
val html = HtmlTag("html")
val body = HtmlTag("body")
val p = HtmlTag("p")
html add body
body add p
println(html.render())
}Solution
Step 1: Understand the infix add function
The infix functionaddadds child tags to the parent's children list, building a tree structure.Step 2: Trace the tag hierarchy and rendering
html adds body, body adds p, so the structure is html > body > p. The render function outputs nested tags accordingly.Final Answer:
<html><body><p></p></body></html> -> Option CQuick Check:
Nested tags render correctly = B [OK]
- Mixing tag closing order
- Assuming infix causes compile error
- Ignoring nested children in render
Ready to prove it?
Pass the test and this pattern moves unknown
