Infix functions let you write code that looks like natural language. They make your code easier to read and write, especially in small, special languages inside Kotlin called DSLs.
Infix functions in DSLs in Kotlin
infix fun functionName(parameter: Type): ReturnType { // function body }
The function must be a member function or an extension function.
It must have exactly one parameter.
times that repeats a string a number of times.infix fun Int.times(str: String) = str.repeat(this)
val result = 3 times "Hi"
infix fun String.shouldBe(expected: String) { if (this != expected) throw AssertionError("Expected $expected but got $this") }
This program defines a Robot class with an infix function moves. It lets you write robot moves "forward" instead of robot.moves("forward"), making it easier to read.
class Robot { infix fun moves(direction: String) = "Robot moves $direction" } fun main() { val robot = Robot() println(robot moves "forward") }
Infix functions improve readability but should be used only when it makes the code clearer.
They can only have one parameter, so they are simple by design.
Use infix functions to create small, easy-to-read DSLs inside Kotlin.
Infix functions let you call functions with one argument without dots or parentheses.
They make code look like natural language, great for DSLs.
Use them to write clearer and more readable Kotlin code.
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
