Kotlin Infix Function Notation With Example

In this tutorial, we will learn about kotlin infix function notation with example. We will see what is infix notation in kotlin, infix notation used in different operations in kotlin, how to create infix notation for any function in kotlin, different criteria used to create infix notation etc.

What is infix notation in kotlin ?

Infix notation in kotlin is a way to call function with infix modifier using infix notation(omitting the dot and the parentheses for the call). For example,

fun main(args: Array<String>) {
   1 shl 2         // called using infix notation,  is same as
   1.shl(2)
}

Here, we have called function shl() as 1 shl 2. Notice that we called the function shl() without using dot and parentheses. This is infix notation in kotlin.

Some of other default infix notation for the functions in kotlin are –
A. a or b for function a.or(b)
B. a and b for function a.and(b)
C. a xor b for function a.xor(b)
D. 1 to “one” for function 1.to(“one”)

Example –

fun main(args: Array<String>) {
   val a  =  1 shl 2
   print(a)
}

When you run the program, you will get output as below –
4
shl is infix notation of shl() function that shifts the value on left by bit counts of numeric value on right side. In this case, 1 is shifted to left by 2 bits. Hence. It will result in 100 in binary representation resulting 4 in decimal.

Let’s take another example –

fun main(args: Array<String>) {
   val a  =  1 xor 2
   print(a)
}

When you run the program, you will get output –
3
xor is bitwise xor operation. Hence output is 3.

What if you want to create your own infix notation for a function? How would you do that? Is there any criteria for it?

How to Create User Defined Kotlin infix function notation

You can create a function with infix notation in kotlin if it meets below criteria –
   – It is a member function or extension function.
   – It accepts only one parameter.
   – It is marked with infix keyword.
   – The parameter must not accept variable number of arguments and must have no default value.

Let’s take an example,

class Utility {
   infix fun square(n: Int): Int {
       return n * n
   }
}

fun main(args: Array<String>) {
   val util = Utility()
   val a  =  util square 2
   print(a)
}

Here, we have created a infix function square. Let’s see if it meets criteria or not.
square function has below criteria –
   – It is member function of Class Utility.
   – It accepts only one parameter.
   – It is marked with infix keyword.
   – It does not accept variable number of arguments and does not have default value.

Hence it meets all requirements for infix notation in kotlin.
When you run the above program, you will get output as below –
4

Notice that we have called square() function using infix notation.

Similarly, you can create an infix function as per your requirement.

Exercises on Kotlin Infix Function Notation

1. Guess the output of below program

class Utility() {
   infix fun upperCase(str: String): String {
       return if (str.isNotEmpty()) str.toUpperCase() else "";
   }
}

fun main(args: Array<String>) {
   val utils = Utility()
   val upper = utils upperCase "Rajeev"
   print(upper)
}

2. Guess the output of below program

class Utility() {
   infix fun upperCase(str: String = "Tutorialwing"): String {
       return if (str.isNotEmpty()) str.toUpperCase() else "";
   }
}

fun main(args: Array<String>) {
   val utils = Utility()
   val upper = utils upperCase "Rajeev"
   print(upper)
}

That’s end of tutorial on Kotlin Infix Function Notation.

Leave a Reply