VOOZH about

URL: https://www.geeksforgeeks.org/kotlin/kotlin-infix-function-notation/

⇱ Kotlin infix function notation - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Kotlin infix function notation

Last Updated : 18 May, 2025

In this article, we will learn about infix notation used in Kotlin functions. In Kotlin, a function marked with infix keyword can also be called using infix notation means calling without using parenthesis and dot. 

There are two types of infix function notation in Kotlin


Standard library infix function notation

When we call operators like and, or , shr, shl etc then compiler looks for the function and calls the desired one. There is a number of standard library infix notations functions but we will discuss here some of them. 

Let's discuss some of the infix notations one by one.

1. Kotlin program using bitwise and operator


Output:

Boolean result1 = true
Boolean result1 = true

Explanation:
Here, we have called the a.and(b) function using infix (a and b). Both produce the same result in the standard output.

2. Kotlin program using the signed shift right(shr) operator


Output:

Signed shift right by 2 bit: 2
Signed shift right by 1 bit: 4

Explanation:
In the above program, we have used signed shift operator. First, performed the operation using the infix notation then performed using dot and parenthesis. 
If we signed shift the value by 2 bits, then 23=8 becomes 2(3-2=1)=2

3. Kotlin program using increment and decrement operators


Output:

9
10
3
2

Explanation:

Here, we have used increment and decrement operators using infix notation. 
++a represents a(8) + 1 so it prints 9 
a.inc() also represents a(9) + 1 so it prints 10 
--b represents b(4) - 1 = 3 
b.dec() also represents b(3)- 1 = 2

User-defined infix function notation

We can create a function with infix notation if the function satisfies the following requirements: 

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

Kotlin program for creating a square function with infix notation


Output:

The square of a number is: 9

Explanation:


In the above program, we have created our infix notation function (m square 3). 

  1. First of all, we defined the infix notation function within the class math because it must be a member function. 
  2. The infix keyword is used to mark the function. 
  3. It contains only one parameter and has no default value and the function return type is also Integer. 
square(n : Int):Int

Then, we create an object for the class math() and called the function using infix notation- 

m square 3

It calculates the square of the number and returns the value 9.


Kotlin program to check the data type of a variable with infix notation


Output: 

Double


Explanation:

We have created an infix notation function to find the datatype of a variable. The datatype has been passed as an argument in an infix call- 

chk dataType 3.3

When checking the data type for the passing parameter, and returns the desired value. Here, it returns a Double to the standard output. 

Precedence of infix function with operators

Precedence matters at the time of execution of an instruction. Infix function calls have lower precedence than the arithmetic operators, type casts, and the rangeTo operator.

The following expressions are equivalent: 

2 shr 1 + 2 and 2 shr (1 + 2)
1 until n * 2 and 0 until (n * 2)
xs union ys as Set<*> and xs union (ys as Set<*>)

On the other hand, infix function call have higher precedence than the boolean operators && and ||, is- and in-checks, and some other operators. 

The following expressions are equivalent as well: 

a xor b || c and a xor (b || c)
a in b xor c and a in (b xor c)


Comment
Article Tags:

Explore