What is an infix function in Kotlin, and what are the requirements a function must satisfy to be callable in infix form?
answer
- Marked `infix`, member or extension, exactly one param
- No vararg, no default value on the param
- Left = receiver, right = argument
- Readability sugar only — same as a.name(b)
- `to`, `until`, `downTo`, `shl`, `and`
basics
~10 sAn infix function lets you call it between two values without a dot or parentheses, like 1 to 2. It must be marked infix, be a member or extension, and take exactly one parameter.
solid answer
~40 sAn `infix` function can be invoked in the form `a foo b` instead of `a.foo(b)`. To qualify it must: (1) be a member function or an extension function; (2) be marked with the `infix` modifier; (3) accept exactly one parameter; (4) that parameter must not be a `vararg` and must not have a default value. The left operand is the receiver, the right is the argument. Common standard-library examples are `to` (builds a `Pair`), `until`, `downTo`, `step`, `shl`, `shr`, `and`, `or`, and `xor`. Infix exists purely for readability — `map.put(k, v)` versus `mapOf(a to b)` reads more like prose. It changes only call syntax, not semantics; you can always still call it with the normal dot form.
code
kotlin · 8 linesinfix fun Int.times2(other: Int): Int = this * other
val a = 6 times2 7 // 42
val b = 6.times2(7) // 42 — dot form still valid
// stdlib infix examples
val p = "key" to "value" // Pair("key", "value")
val r = (1 until 5).toList() // [1, 2, 3, 4]go deeper
States the infix modifier, member-or-extension, exactly-one-param rule, and gives to as an example.
Adds the no-vararg / no-default constraint and notes receiver-vs-argument and that the dot form still works.
Frames infix as pure readability sugar compiling to a.name(b) and distinguishes it from operator overloading.
Discusses when infix improves API ergonomics vs. when it harms clarity, and library design conventions around it.
## What an infix function is An **infix function** is a function you can call by placing its name *between* two operands, with no dot and no parentheses: ```kotlin infix fun Int.shl(bits: Int): Int = this shl bits // (conceptual) val pair = 1 to 2 // instead of 1.to(2) val shifted = 1 shl 4 // instead of 1.shl(4) -> 16 ``` The value on the **left** of the function name is the **receiver** (`this`), the value on the **right** is the single **argument**. ## The four rules A function is eligible for infix calls only if ALL of these hold: - It is a **member function** (declared inside a class/object) **or** an **extension function** (declared as `fun Receiver.name(...)`). Top-level functions with no receiver cannot be infix. - It is marked with the **`infix`** modifier keyword. - It has **exactly one parameter**. - That parameter is **not `vararg`** and has **no default value**. ```kotlin infix fun String.repeated(times: Int): String = this.repeat(times) val s = "ab" repeated 3 // "ababab" val s2 = "ab".repeated(3) // identical — dot form always works ``` ## Why it exists Infix is **syntactic sugar for readability only**. It does not change dispatch, performance, or semantics. The classic motivation is DSL-like, prose-like code: `mapOf("a" to 1, "b" to 2)` reads naturally because `to` is infix. Range builders `1 until 10`, `10 downTo 1`, `1..10 step 2` rely on it too. ## Things that are NOT infix - Operators like `+` use **operator overloading** (the `operator` modifier), a different mechanism. - You still need the receiver and argument to be unambiguous; mixing infix calls with other operators requires understanding precedence (covered separately). Under the hood the compiler simply rewrites `a name b` into `a.name(b)`.
- Can a top-level function with no receiver be infix?No. Infix functions must be a member or an extension, so they always have a receiver as the left operand.
- Does adding `infix` remove the ability to call with a dot?No. `a foo b` and `a.foo(b)` are both valid; `infix` only adds the dot-less form.
Like writing '3 plus 4' in English instead of 'plus(3, 4)' — same operation, more readable word order.
saying these in an interview costs you the question
- Claiming any function can be infix
- Saying infix functions can take multiple parameters
- Confusing infix with operator overloading (`operator` modifier)
- Thinking infix changes performance or dispatch
- Allowing a default value or vararg on the infix parameter