Kotlin Infix Function Call

In this article, you will learn about infix notation to call a function in Kotlin with the help of examples.

Kotlin supports function calls of a special kind, called infix calls.

In Kotlin, functions marked with infix keyword can also be called using infix notation means calling without using parenthesis and dot.

Before you learn how to create a function having infix notation, let’s explore some of the commonly used built-in infix functions.

Following are a few common examples of infix notations in Kotlin:

Infix Notation Example – Creating a Map

fun main(args: Array) {

   //to() is an infix function
   //used while working with Map
    val myMap = mapOf(1 to "one", 2 to "two", 3 to "three")
    for (value in myMap.values) {
        println(value)
    }
}
Output:
one
two
three

Infix Notation Example – Boolean Operators ( and,  or )

fun main(args: Array) {

    val a = true
    val b = false
    var result: Boolean

    //or, and are infix functions
    //performs logical 'and','or' operations between the two values
    result = a or b // a.or(b)
    println("result = $result")

    result = a and b // a.and(b)
    println("result = $result")
    
}
Output:
result = true
result = false

Infix Notation Example – String.matches()

fun main(args: Array) {
    val regex = Regex("[tT]rue|[yY]es")
    val str = "Yes"

    //matches is an infix function
    //used while working with String
    //returns `true` if the string matches the given regular expression.
    val result = str matches regex  //str.matches(regex)
    print("result = $result")
}
Output:
result = true

Infix Notation Example – Range Operators (until, downTo, step)

fun main(args: Array) {

   //until, downTo, step are infix functions 
   //used while working with Range
    print("for (i in 1 until 5) print(i) = ")
    for (i in 1 until 5) print("$i ")

    println()
    
    print("for (i in 10 downTo 1 step 3) print(i) = ")
    for (i in 10 downTo 1 step 3) print("$i ")
}
Output:
for (i in 1 until 5) print(i) = 1 2 3 4 
for (i in 10 downTo 1 step 3) print(i) = 10 7 4 1

Creating a function with infix notation

You can make a function call in Kotlin using infix notation if the function

  • is a member function or extension function.
  • has a single parameter.
  • marked with infix keyword.
  • The parameter must not accept variable number of arguments and must have no default value.

Example: User-defined function with infix notation

class Math {
    //user defined infix member function
    infix fun cube(n: Int): Int {
        val num = n * n * n
        return num
    }
}

fun main(args: Array) {
    val math = Math()
    //calling cube() without using infix notation
    //math.cube(5)

    //calling cube() using infix notation
    val result = math cube 5   
    print("The cube of number 5 is: " + result)
}
Output:
The cube of number 5 is: 125
Example explained:
  • cube() is a member function of class Math, accepts only one parameter, and marked with infix keyword.
  • Now cube() is an infix function we can call it by using infix notation like this: math cube 5 which is equivalent to math.cube(5).

Leave a Reply