We've all heard of extension functions in Kotlin, but have you used an infix function?

We've all heard of extension functions in Kotlin, but have you used an infix function?
Photo by Tra Nguyen / Unsplash

An infix function provides better readability for operations that require two arguments/objects.

It also operates the same way as an extension function. This means you can reference the fields of the receiver (in this example it's the Session class) implicitly without calling "this".

In the example below, I use the belongTo() function both with and without infix notation. As you can see, the infix function removes the extra parentheses. Infix makes expressions a bit more readable, and it can make a difference in a large codebase.
The only requirement for the infix function is that you must have a receiver (Session) and a parameter (Chapter).

Subscribe to Multiplatform Mindset