Jagdeep Singh
Jagdeep's blog

Jagdeep's blog

The fun of Kotlin — Infix Functions

The fun of Kotlin — Infix Functions

Jagdeep Singh's photo
Jagdeep Singh
·Apr 4, 2021·

2 min read

The fun of Kotlin — Infix Functions

In the previous part of this series, we learned about Local functions in Kotlin. I this part, I’d like to talk about Infix functions. Infix notation makes function calls look super awesome.

Pit stops

  1. Local functions.
  2. Infix functions. (you’re here)
  3. Anonymous functions.
  4. Inline functions.
  5. Returns and local returns.

Who should read?

This post is for all the Kotlin lovers and those people who are learning or want to learn Kotlin.


I’d recommend that, before moving forward, you have basic knowledge of Kotlin language syntax such as how to define variables, classes and functions. Official website of Kotlin is the best place to learn the basic syntax of the language.

Infix functions

Functions marked with the infix keyword can also be called using the infix notation (omitting the dot and the parentheses for the call). Infix functions must satisfy the following requirements:

Example of infix notation in Kotlin

infix fun String.shouldBeSame(other: String) = this == other

// calling the function using the infix notation
"bello" shouldBeSame "bello"

// is the same as

On the left side, is the receiver instance on which you’ll define infix function. On the right side, is a parameter that will be passed to the infix function.

I think the best use case for infix notation is to make argument matchers in testing frameworks more readable and create nice looking APIs.

Example — Kotlin test framework


Kotlintest is a testing framework for Kotlin. It uses infix notation extensively to make really nice looking APIs. Here is a snippet of their API.

Clean API of kotlintest using infix notation

"substring" should include("str")

user.email should beLowerCase()

a + b shouldBe result

Official documentation of Infix notation

Checkout more examples of Kotlin function in my sample GitHub project.


Thank you for taking your time to read this article. If you have any questions, suggestions on how to improve this articles, please feel free to contact me on twitter :)

Jagdeep Singh

Share this