Infix notation in Kotlin
- Authors
- Name
- Amit Shekhar
- Published on
I am Amit Shekhar, Co-Founder @ Outcome School, I have taught and mentored many developers, and their efforts landed them high-paying tech jobs, helped many tech companies in solving their unique problems, and created many open-source libraries being used by top companies. I am passionate about sharing knowledge through open-source, blogs, and videos.
Join Outcome School and get high paying tech job: Outcome School
Before we start, I would like to mention that, I have released a video playlist to help you crack the Android Interview: Check out Android Interview Questions and Answers.
In this blog, we will learn about the Infix notation in Kotlin. The Infix notation enables us to write code that looks much more like a natural language.
What is Infix notation in Kotlin?
Infix notation in Kotlin allows a function to be called without the use of the dot and parentheses.
We will understand this with an example.
Let's create a function add
and mark it with the infix
keyword as below:
infix fun Int.add(value: Int): Int = this + value
As we have read above that the functions marked with the infix
keyword can also be called using the infix notation. It means that we can omit the dot and the parentheses.
This is how we can call this using infix notation.
val sum = 5 add 10
Now, the code looks much more like a natural language.
And I am sure that, you must have used somewhere like below in your Kotlin code earlier.
val map = mapOf(
1 to "Amit Shekhar",
2 to "Anand Gaurav",
3 to "Lionel Messi"
)
mapOf
needs the list of pairs: Pair<K, V>
.
Here to
for the Pair is possible only because of the infix.
When we check the source code of the Pair
in Kotlin, we will find the following.
infix fun <A, B> A.to(that: B): Pair<A, B> = Pair(this, that)
This is why we are able to create Pair
using infix notation.
Now, it is also important to know about the precedence of the Infix function calls.
- Infix function calls have lower precedence than arithmetic operators. It means that
5 add 10 + 15
is equivalent to5 add (10 + 15)
- Infix function calls have higher precedence than the boolean operators
&&
and||
. It means thata && b xor c
is equivalent toa && (b xor c)
.
Rules that we need to follow while creating Infix functions:
- They must be member functions or extension functions.
- They must have a single parameter.
- The parameter must not accept a variable number of arguments.
- The parameter must not have a default value.
The following are NOT correct as they are not following the above rules.
- Have more than one parameter.
infix fun Int.add(a: Int, b: Int): Int // WRONG
- Have a variable number of arguments.
infix fun Int.add(vararg numbers: Int): Int // WRONG
- Have a default value for the parameter.
infix fun Int.add(value: Int = 10): Int // WRONG
So, we need to keep the rules in mind while creating them. This is how we can use infix notation in Kotlin.
Prepare yourself for Android Interview: Android Interview Questions
That's it for now.
Thanks
Amit Shekhar
Co-Founder @ Outcome School
You can connect with me on:
Follow Outcome School on: