- Phuc's Notes
- Posts
- Infix functions in Kotlin
Infix functions in Kotlin
Learn Infix functions in Kotlin
Kotlin is known for being expressive and concise. Recently, I reviewed Kotlin again and one of the cool features I encountered was the infix functions. This feature helps our code look more like natural language.
In this blog, let's explore together what infix functions are, how to create them, and most importantly why they work the way they do.
๐ 1. What is an infix function?
To understand the feature easier, let's start with the normal way to call function that we almost do every day:
/**
* An extension function of String.
* Repeat the string N times
*/
fun String.repeatN(times: Int): String {
require(times >= 0)
return this.repeat(times)
}
fun main() {
val kotlin = "Kotlin."
print(kotlin.repeatN(3)) // Output: Kotlin.Kotlin.Kotlin.
}
With this normal way, we will need to use the dot .
and the parentheses ()
when calling it. But did you know that you can call this kind of function without the dot and the parentheses? The answer is Yes! In Kotlin you can write the code like this:
// The only difference: The function signature starts with the "infix" keyword
infix fun String.repeatN(times: Int): String {
require(times >= 0)
return this.repeat(times)
}
fun main() {
val kotlin = "Kotlin."
// Normal way
println(kotlin.repeatN(3)) // Output: Kotlin.Kotlin.Kotlin.
// Use infix syntax
println(kotlin repeatN 3) // Output: Kotlin.Kotlin.Kotlin.
}
It looks more natural! Here is the syntax to call the infix function:
receiver infixFunctionName parameter
| | |
kotlin repeatN 3
So if someone asks you "What is an infix function in Kotlin?", you may answer:
An infix function is a function that can be called without using the dot .
and without parentheses ()
But it will be better if we can give more clarification. The fact is we can not use the infixkeyword for all functions. Did you notice that I previously highlighted the phrase "this kind of function"? That means a function can only be an infix function if it satisfies some rules. Let's move on and see the rules immediately.
๐ 2. The Rules of Using Infix Functions
Rule #1: They must be member functions or extension functions.
Rule #2: They must have a single parameter and must not accept variable number of arguments (vararg
)
Rule #3: The infix functions must explicitly use infix
keyword
Let's div into each rule and understand why they exist.
Rule #1: They must be member functions or extension functions
This rule means an infix function either:
Be defined inside a class (member function)
OR
Extend and existing type (extension function)
Let's look at the example we created above:
fun main() {
val kotlin = "Kotlin."
// Normal way
println(kotlin.repeatN(3)) // Output: Kotlin.Kotlin.Kotlin.
// Use infix syntax
println(kotlin repeatN 3) // Output: Kotlin.Kotlin.Kotlin.
}
Here in this example:
The variable
kotlin
is a receiverThe
repeatN
is the infix functionThe number
3
is the sing parameter passed to the function
That's why we can not use infix for a top-level function (not inside a class). If you do that, you defintely get an error:

infix is not applicable for top-level function
Rule #2: They must have a single parameter and must not accept variable number of arguments (vararg
)
Kotlin requires exactly one argment when calling an infix function (number 3 in our example). Similarly vararg
is not allowed because with vararg
we can pass multiple arguments to the function.

infix functions only allow single parameter

vararg parameter is not allowed
Rule #3: The infix functions must explicitly use infix
keyword
This is quite obvious, because if we don't declare the function with infix
Kotlin will treat it as a normal function and we can not call it using infix syntax.
In the official document, the Kotlin team gives another requirement for the infix function: "The parameter ... must have no default value". But I don't think this statement is correct, because an infix function can be used as a normal function: receiver.funcName(singleParam). If we have a default value for the single parameter, it's completely valid. Just in case we use the infix syntax when calling the function, the default value will be ignored, because we must always provide the argument (passed value for the single parameter).
๐ฅ 3. Common built-in infix functions in Kotlin
FUNCTION | USAGE EXAMPLE | DESCRIPTION |
---|---|---|
to | "key" to "value" | Creates a Pair. Common in mapOf(...) or for ((k, v) in map) |
downTo | 5 downTo 1 | Creates a decreasing range: 5, 4, 3, 2, 1 |
rangeTo | 1..5 or 1 rangeTo 5 | Creates an increasing range: 1, 2, 3, 4, 5 |
step | 1..10 step 2 | Adds a step to a range (e.g., 1, 3, 5, 7, 9) |
shl, shr, ushr | 1 shl 2 | Bitwise shift left/right (like <<, >>, >>> in Java) |
and, or, xor | a and b, a or b | Bitwise operations |
until | 0 until 5 | Creates a range that excludes the end (0 to 4) |
contains (operator) | "Kotlin" in list | Checks if a value is in a collection (this works because in is infix) |
๐ซ 4. When not to use infix functions
Although infix function is cool and help the code looks more natural (if used wisely), overusing it can make the code less readable and hard to use. For example:
user update data // โ What exactly is happening here?
data apply 5 // โ What is "apply" doing here?
Conclusion
Infix functions are Kotlinโs way of letting your code speak like a sentence. Used wisely, they can boost readability and charm. Used carelessly, they can make code look like riddles. When your function acts like a relationship between two things, infix style might just be the best fit.
Happy coding! See you in another blog!
Reply