Kotlin 101 - Top Level and Infix functions

Kotlin 101 - Top Level and Infix functions


In this blog post, we discuss two interesting Kotlin features: top-level functions and infix functions. Top-level functions can be defined directly in a file without the need for a class and can be accessed from different classes or files. Infix functions can extend classes and interfaces with new functionalities and must have a single parameter. These features show that Kotlin has a lot to offer beyond Java.

After spending most of my career as a Java developer, I’ve recently changed and moved to the different but similar world of Kotlin. While it is much easier for a Java developer to make a shift to Kotlin instead of Python or Go, beneath the surface, there are lots of new things to be explored. In this blog miniseries, I’ll write about topics that have left an impression on me and I found interesting in my ongoing Kotlin exploration journey. Today we’ll learn more about different functions in Kotlin.

Top Level Functions

I’m sure that anyone who has contributed more seriously to at least one Java project, has found themselves writing utility methods in a helper class. These helper classes usually don’t have any other purpose than acting as a “container class” for the methods. One example of that is the Collections class part of java.util package.

The news in Kotlin are - functions are considered to be “first class citizens”, meaning functions can be defined directly in the file, without previously creating a class.

// inside a file named Utils.kt
fun myFancyTopLevelMethod(a: int, b: int) = a + b;

One interesting finding for me was that under the hood, the compiler creates a class file for us which is named by the name of the file. This function can be accessed from different classes or files without the need to create an instance or reference a class to call this function.

Infix Functions

Infix functions I have been mostly using as a “extensions function”. Extension functions are also a cool Kotlin feature. Have you ever ended up wishing the third party class that you’re using to provide additional functionality? I know it happened to me a few times.

Kotlin provides the option to extend classes and interfaces with new functionalities. So adding a new method to the String class using the infix function would be:

// compare the length of two Strings
infix fun String.equalLength(other: String) = this.length == other.length

This method can be called on any String value in two different ways:

  • Using the traditional way of calling methods: "1".equalLength("23")
  • Using the infix notation: "1" equalLength "23"

One rule that needs to be taken into consideration when working with infix functions is that they must always have a single parameter. Also, as it can be noticed in our example, when we want to operate with the caller of the method in an infix function, we are using this keyword.

For me, these two new different ways of doing stuff in the Koltin world comparing to what Java offers are are a great start and indicator that there is much more that this language has to offer and we should try to fit and tailor our code based only on the Java knowledge. Stay tuned for exploring the differences in the Lambdas.