Infix Functions

Discover what infix functions are and how you can implement your own to write more readable Kotlin code.

Infix functions are a great tool to improve readability of function calls in certain cases.

What are Infix Functions? #

In programming, function calls are typically written in prefix notation, meaning that the function name is in front, followed by its arguments in parentheses:

gcd(54, 24)

With infix functions, however, the function name stands between the arguments. You’ve already seen this with the predefined to function, which creates Pairs (to put into a map):

"Dwayne" to 3.14159

Here, the function name to is surrounded by its two arguments, which brings us to the first limitation of infix functions: an infix function must have exactly two parameters. In the function call, the first argument is on the left ...