← Back to Kotlin Course | Chapter 10: Extension Functions & Properties | Lesson 5 of 6

Infix Extension Functions

Combining infix with extensions lets you invent your own natural-reading operators for any existing type, like a custom word placed between two values.

Writing an Infix Extension

Combining infix with fun ReceiverType.name(param: Type) adds an operator-like call syntax to an existing type, usable as receiver name argument.

Example: Writing an Infix Extension

markup
infix fun Int.plusPercent(percent: Int): Int = this + (this * percent / 100)

fun main() {
    val price = 100
    println("Price with 20% added: ${price plusPercent 20}")
}

Building Readable Domain Logic

Infix extensions are often used to give domain-specific operations a natural, sentence-like reading, which can make specialized code much clearer than a plain method call.

Example: Building Readable Domain Logic

markup
infix fun Int.daysAfter(startDay: Int): Int = (startDay + this) % 7

fun main() {
    val monday = 1
    println("3 days after Monday: ${3 daysAfter monday}")
}

Infix Extensions on Custom Types

Infix extensions work on your own classes too, letting you define natural-reading operations between instances of types you control.

Example: Infix Extensions on Custom Types

markup
data class Vector(val x: Int, val y: Int)

infix fun Vector.dot(other: Vector): Int = x * other.x + y * other.y

fun main() {
    val v1 = Vector(2, 3)
    val v2 = Vector(4, 5)
    println("Dot product: ${v1 dot v2}")
}

When to Avoid Infix Style

Infix notation reads best for operations that resemble a natural verb or relationship between two values; for anything else, a regular method call with a clear name is more appropriate.

Note: Prefer a regular method name like combineWith() over infix style if the relationship being expressed isn't intuitive from the words alone.

Example: When to Avoid Infix Style

markup
infix fun String.repeatedTwice(separator: String): String = "$this$separator$this"

fun main() {
    println("Hi" repeatedTwice ", ")
}
Common Mistakes
  1. Forgetting that an infix extension function still must take exactly one parameter, the same rule as any other infix function.
  2. Applying infix extension notation to operations that are not naturally symmetric or relational, hurting readability instead of helping.
  3. Not marking the extension function infix, then being confused why the operator-like call syntax a op b does not compile.
Chapter Summary
  • Combining infix with an extension function lets an existing type gain a brand-new operator-like syntax.
  • The extension must still satisfy the infix requirement of exactly one parameter.
  • Infix extensions are commonly used to create small domain-specific languages (DSLs) that read naturally.
  • As with any infix function, this style should be reserved for operations that genuinely read like a natural relation.
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.