Kotlin Tutoial
Basics
Control Flow
Array & String
Functions
Collections
OOPs Concept
Exception Handling
Null Safety
Regex & Ranges
Java Interoperability
Miscellaneous
Android
In Kotlin, infix notation is a way to call a function on an object with a single parameter without using dots and parentheses. This makes certain code constructs more readable and expressive. This is particularly useful for creating domain-specific languages (DSLs) within Kotlin.
Here's a tutorial on the infix function notation in Kotlin:
To declare a function as an infix function, you use the infix
keyword:
class Person(val name: String) { infix fun greet(greeting: String) { println("$greeting, $name!") } } fun main() { val john = Person("John") john greet "Hello" // This uses infix notation }
When you run the main
function, it prints:
Hello, John!
You can also use infix notation with extension functions:
infix fun String.concat(other: String): String { return this + other } fun main() { val result = "Hello" concat " World" println(result) // Prints: Hello World }
This can be particularly useful when you want to create custom operators or improve code readability:
data class Point(val x: Int, val y: Int) { infix fun until(to: Point): List<Point> { val xs = x until to.x val ys = y until to.y return xs.flatMap { x -> ys.map { y -> Point(x, y) } } } } fun main() { val start = Point(1, 1) val end = Point(3, 3) val points = start until end println(points) // Prints: [(1, 1), (1, 2), (2, 1), (2, 2)] }
infix
keyword.Infix notation isn't just a gimmick. It can improve code readability in certain scenarios:
Infix notation in Kotlin can make code more expressive in certain scenarios. However, it's important to use it judiciously. Overusing infix functions, or using them in contexts where they don't improve readability, can make code more confusing.
Using infix functions in Kotlin:
infix fun Int.addCustom(value: Int): Int { return this + value } val result = 5 addCustom 3
Infix notation vs regular function calls in Kotlin:
val regularResult = 5.addCustom(3)
Benefits of infix function notation in Kotlin:
if (result isEqual 8) { println("Equal") }
Creating custom infix functions in Kotlin:
infix fun String.combine(other: String): String { return this + other } val combined = "Hello" combine "World"
Scenarios for using infix notation in Kotlin:
val sum = 5 addCustom 3
Infix functions with multiple parameters in Kotlin:
infix fun Int.multiplyAndAdd(value: Int): Int { return this * value + value } val result = 5 multiplyAndAdd 3
Limitations and considerations for infix functions:
// Incorrect: Multiple parameters infix fun Int.invalidInfix(value1: Int, value2: Int): Int { return this + value1 + value2 }
Using infix functions with standard library functions in Kotlin:
val concatenated = "Hello" to "World"
Infix functions and readability in Kotlin code:
val sum = 5 addCustom 3
Infix functions and operator overloading in Kotlin:
operator fun Int.plusCustom(value: Int): Int { return this + value } val result = 5.plusCustom(3) // Regular call val infixResult = 5 plusCustom 3 // Infix notation
Infix functions in extension functions in Kotlin:
infix fun String.prepend(prefix: String): String { return prefix + this } val result = "World" prepend "Hello"