Kotlin Infix Function Notation

This Story is from the Kotlin- Series, In this article, we will learn infix notation used in Kotlin functions.

Kotlin is a language that adds many fresh features to allow writing cleaner, easier-to-read code.This, in turn, makes our code significantly easier to maintain and allows for a better end result from our development. Infix notation is one of such features.

In Kotlin, a functions 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-

  1. Standard library infix function notation.
  2. User defined infix function notation.

Standard infix function notation –

The most commonly seen standard infix function is inline Map definition.

map( 1 to “one”, 2 to “two”, 3 to “three” )

“to” might look like a special keyword but in this example, this is a to() method leveraging the infix notation and returning a Pair<A, B>.Apart from the to() function, used to create Pair<A, B> instances, there are some other functions that are defined as infix.

For example, the various numeric classes — Byte, Short, Int, and Long — all define the bitwise functions and(), or(), shl(), shr(), ushr(), and xor(), allowing some more readable expressions:

The Boolean class defines the and(), or() and xor() logical functions.The String class also defines the match and zip functions as infix, allowing some simple-to-read code

Standard infix function Example :

fun main() {
var a = 15
var b = 12
var c = 11

var result1 = (a > b) or (c > b)
println("Boolean result1 = $result1")

var result2 = (a > b).and(c > b)
println("Boolean result1 = $result2")

var result3 = (a > b) and (a > c)
println("Boolean result2= $result3")

var result4 = (a > b).xor(false)
println("Boolean result3 = $result4")

var result5 = a shr 2
println("Signed shift right by 2 bit: $result5")

var result6 = a.shr(1)
println("Signed shift right by 1 bit: $result6")

println(a.inc() + 1)

println(a.dec()-2)
}

User defined infix function notation –

We can create own function with infix notation if the function satisfy the following requirements:

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

User defined infix Example:

class math() {
infix fun square(n: Int): Int {
val number = n * n
return number
}

infix fun divide(value: Int): Int {
val number = value / value
return number
}

infix fun validateDataType(x: Any): Any {
var i = when (x) {
is String -> "String"
is Int -> "Integer"
is Double -> "Double"
else -> "invalid"
}
return i
}
}

fun main() {
val m = math()
val result = m square 3
var result1 = m validateDataType 3.3
println(result)
println(result1)
}

This quick tutorial shows some of the things that can be done with infix functions, including how to make use of some existing ones and how to create our own to make our code cleaner and easier to read.

That’s all folks! Feel free to comment or message me if you have any questions, suggestions or ideas! 🙏

Senior Android Developer. Working on technology Like Java,Kotlin, JavaScript.Exploring Block Chain technology in simple words.