Kotlin Infix Functions
Most function calls use dots and parentheses, such as a.plus(b). An infix function drops both and reads like a sentence: a plus b. Infix functions turn code into friendly phrases and build small, readable mini-languages.
Reading Code Like English
A good sentence has a subject, a verb, and an object. An infix call follows the same pattern.
subject verb object ------- ------ -------- 1 to "one" 10 downTo 1 5 shl 2
Kotlin ships with several infix functions already. The to function builds a pair of two values. The until function builds a range. The step function sets the jump size.
val pair = "India" to "Delhi"
println(pair.first) // India
println(pair.second) // Delhi
for (i in 10 downTo 1 step 3) {
print("$i ") // 10 7 4 1
}Writing Your Own
The keyword infix marks a function for this style. Three rules apply.
- The function belongs to a class or acts as an extension of one.
- The function takes exactly one parameter.
- The parameter has no default value and is not a vararg.
infix fun Int.percentOf(total: Int): Double {
return this * 100.0 / total
}
fun main() {
val result = 25 percentOf 200
println(result) // 12.5
}The call 25 percentOf 200 equals 25.percentOf(200). Both forms work. The infix form reads better.
Infix Inside a Class
A member function can carry the keyword as well. A small Money class shows the idea.
class Money(val amount: Int) {
infix fun plus(other: Money): Money {
return Money(amount + other.amount)
}
}
fun main() {
val wallet = Money(50) plus Money(30)
println(wallet.amount) // 80
}Building a Mini Language
Infix functions create tiny vocabularies. A test helper reads almost like plain English.
infix fun Int.shouldBe(expected: Int) {
if (this == expected) println("PASS") else println("FAIL: $this is not $expected")
}
fun main() {
(2 + 3) shouldBe 5 // PASS
(2 + 3) shouldBe 6 // FAIL: 5 is not 6
}How Precedence Works
Operators follow an order of priority. Infix calls rank lower than arithmetic and higher than comparison. Arithmetic runs first, the infix call runs second, and comparisons run last.
Highest * / %
+ -
ranges (..)
infix functions (to, shl, percentOf)
elvis ( ?: )
in, is
< > <= >=
Lowest == !=
println(1 + 2 shl 3) // (1 + 2) shl 3 = 24Parentheses remove any doubt. Add them whenever a line mixes several operations.
The Receiver Rule
An infix call always needs an explicit receiver on the left side. Inside a class, write this plus other instead of plus other.
Why Readability Matters
People read code far more often than they write it. A line such as price percentOf total tells the reader the intent at a glance. A dotted call asks the reader to decode brackets and dots before the meaning appears. Infix style removes that small effort, and the saving grows across a large project.
Infix functions pair well with extension functions. An extension adds a new ability to an existing type, such as Int or String, without touching the original code. Marking that extension as infix lets the new ability read like a native word of the language.
Best Practices
- Choose verbs or short phrases for names, so the line reads naturally.
- Use infix for operations with a clear left side and right side.
- Avoid infix for actions that change state in surprising ways.
- Keep the vocabulary small. Too many custom words confuse teammates.
Infix or Normal Call
| Style | Example | Best For |
|---|---|---|
| Normal | a.percentOf(b) | Complex calls and chains |
| Infix | a percentOf b | Readable two-sided relations |
