0Pricing
Kotlin Academy · Lesson

Infix Functions

Readable function calls.

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)
}

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