Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Infix Functions

Infix notation is a particular way of calling an extension function or member function, in which the call operator (the dot) and the parentheses are omitted. This can help to make the code more readable.

You’ve already seen an example of this:

for (n in 10 downTo 1) {
    ...
}

Here, 10 downTo 1 is infix notation for the call 10.downTo(1), which invokes the downTo() extension function on the value 10, passing it an argument of 1. This function call returns an integer progression representing the sequence 10, 9, 8, … 2, 1.

To create a function that supports infix notation, begin its definition with the keyword infix. Note also that your function must

  • Be an extension function or member function
  • Have a single parameter, for which an argument is always supplied

Example

Consider an extension function for strings, longerThan(). This function returns true if the receiver contains more characters than the string supplied as an argument:

fun String.longerThan(str: String) = this.length > str.length

This function could be used on two strings a and b like so:

if (a.longerThan(b)) {
    ...
}

To enable the use of infix notation here, all we need to do is add the infix keyword to the start of the function definition:

infix fun String.longerThan(str: String) = this.length > str.length

Now the function call can be simplified to

if (a longerThan b) {
    ...
}

[Try this out]

Note

Obviously, this is not really necessary.

We could just compare the string lengths directly with a.length > b.length.

The benefit here is the increase in clarity. a longerThan b is a bit clearer and more readable than the explicit comparison of properties.

You shouldn’t overuse infix functions or extension functions, but sometimes it may be worth introducing them to make code more readable.

Task 5.5

  1. Copy Anagrams.kt from the task5_1_1 subdirectory of your repository to the task5_5 subdirectory.

  2. Edit this new copy of Anagrams.kt and modify the anagram checking function so that it is an extension function of String, callable using infix notation. Change the function’s name to anagramOf.

  3. Modify main() so that it uses this new version of the function. If you’ve done everything correctly, it will be possible to test whether one string is an anagram of another using very readable code like this:

    if (secondString anagramOf firstString) {
        println("$firstString and $secondString are anagrams!")
    }
    
  4. Compile the program and run it to check that it behaves as expected.