0Pricing
Kotlin Academy · Lesson

Infix Functions

Readable function calls.

Infix Functions is a free Kotlin Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What Is an Infix Function?

An infix function can be called without the dot or parentheses: a func b. Mark it with the infix keyword.

infix fun Int.times2(factor: Int) = this * factor

fun main() {
    println(3 times2 4)
}

Rules for Infix

An infix function must be a member or extension function, take exactly one parameter, and that parameter must not be vararg or have a default.

infix fun Int.plusBy(other: Int) = this + other

fun main() {
    println(5 plusBy 7)
}

Built-in Infix Functions

Many standard functions are infix: to (builds a Pair), and, or, shl, and until, downTo, step for ranges.

fun main() {
    val pair = "key" to 42
    println(pair)
    for (i in 1 until 4) print(i)
}

Building a Pair with to

The familiar "a" to 1 map syntax is just the infix to function returning a Pair.

fun main() {
    val map = mapOf(1 to "one", 2 to "two")
    println(map[2])
}

Infix as a Member Function

Infix functions can be members, letting you write fluent domain expressions.

class Money(val cents: Int) {
    infix fun add(o: Money) = Money(cents + o.cents)
}

fun main() {
    val total = Money(100) add Money(50)
    println(total.cents)
}

Readability First

Infix shines when the call reads like a sentence. Choose verb-like names so a name b flows naturally.

infix fun String.repeatedTimes(n: Int) = this.repeat(n)

fun main() {
    println("ab" repeatedTimes 3)
}

Precedence of Infix Calls

Infix calls have lower precedence than arithmetic but higher than comparison. Add parentheses when mixing to stay clear.

infix fun Int.combine(o: Int) = this * 10 + o

fun main() {
    println(1 combine 2 + 3)
}

The this Receiver

In an infix extension, this is the left operand; the parameter is the right operand.

infix fun Int.minus2(o: Int) = this - o

fun main() {
    println(10 minus2 3)
}

Chaining Infix Calls

Infix calls are left-associative, so you can chain them.

infix fun Int.then(o: Int) = this + o

fun main() {
    println(1 then 2 then 3)
}

Avoid Overusing Infix

Infix removes dots and parentheses, which can hurt clarity for non-obvious operations. Use it only where it reads better.

infix fun Int.power(exp: Int): Int {
    var r = 1
    repeat(exp) { r *= this }
    return r
}

fun main() {
    println(2 power 5)
}

A Fluent Example

Infix can make assertions or builders read almost like English.

infix fun String.shouldStartWith(prefix: String) =
    this.startsWith(prefix)

fun main() {
    println("kotlin" shouldStartWith "kot")
}

Quick Check

Test your understanding of infix functions.

Recap

You learned infix functions:

  • Mark with infix and call as a func b.
  • Must be a member/extension with one non-vararg, non-default parameter.
  • to, until, downTo are built-in infix functions.
  • Use it where it genuinely improves readability.
infix fun Int.add(o: Int) = this + o

fun main() {
    println(4 add 6)
}

Frequently asked questions

Is the “Infix Functions” lesson free?

Yes — the full text of “Infix Functions” is free to read here on the web, and the Kotlin Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Kotlin Academy course, upgrade to CoddyKit PRO.

What will I learn in “Infix Functions”?

Readable function calls. You practise Kotlin Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Kotlin Academy?

No prior experience is required. Kotlin Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Infix Functions” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Kotlin Academy lesson?

Yes. Every Kotlin Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Infix Functions
  2. Building DSL-like APIs
  3. tailrec Functions
  4. When to Use Each
← Back to Kotlin Academy