In Kotlin, what does the `operator` keyword do, and how does an expression like `a + b` connect to a function?
answer
- `operator` modifier + reserved name
- `a + b` -> `a.plus(b)`
- By name, no Addable interface
- Member OR extension
- Can't invent new operators
basics
~10 sOperators like + or [] are shortcuts. Kotlin turns them into calls to specially named functions you mark with the operator keyword. So a + b actually calls a.plus(b).
solid answer
~40 sKotlin operators are syntactic sugar for functions with fixed, reserved names. Writing `a + b` compiles to `a.plus(b)`; `a * b` to `a.times(b)`; `a[i]` to `a.get(i)`; `a[i] = v` to `a.set(i, v)`; `a()` to `a.invoke()`; `a in b` to `b.contains(a)`. To make a function usable this way you must declare it with the `operator` modifier and the exact reserved name and signature; otherwise the compiler rejects the operator form. The mapping is name-based, not interface-based — there's no `Addable` interface; the compiler simply looks for an `operator fun plus`. These can be member functions or extension functions, which lets you add operators to types you don't own (e.g. an `operator fun BigDecimal.plus`).
code
kotlin · 6 linesdata class Money(val cents: Long) {
operator fun plus(o: Money) = Money(cents + o.cents)
}
val total = Money(150) + Money(250) // -> Money(400)
// equivalent: Money(150).plus(Money(250))go deeper
Knows + maps to plus and that operator is required; can write a simple plus.
Recalls the full mapping table and that resolution is by name, not interface; uses extensions.
Explains why name-based resolution keeps things statically typed and where it differs (e.g. in receiver), and reasons about readability trade-offs.
Frames operator overloading as an API-design tool, weighs abuse risks, and sets team conventions for when domain operators improve vs. obscure code.
## What operator overloading means in Kotlin Kotlin lets you give a predefined set of symbolic operators (`+`, `*`, `[]`, `()`, `in`, `<`, etc.) custom meaning for your own types. This is called **operator overloading**. Unlike some languages, Kotlin does **not** let you invent new operators or change precedence — you may only provide implementations for a fixed list of operators by writing functions with **reserved names**. ## The `operator` keyword The `operator` modifier marks a function as the implementation of an operator. Two things must line up: - The function must have the **exact reserved name** for that operator (e.g. `plus` for `+`). - It must have a **conforming signature** (right number/kind of parameters). ```kotlin data class Vec(val x: Int, val y: Int) { operator fun plus(other: Vec) = Vec(x + other.x, y + other.y) } val c = Vec(1, 2) + Vec(3, 4) // compiles to Vec(1,2).plus(Vec(3,4)) -> Vec(4, 6) ``` If you write `fun plus(...)` **without** `operator`, you can still call `a.plus(b)` by name, but `a + b` will **not** compile. Conversely, marking a wrongly-named function `operator` is a compile error. ## How an expression maps to a call The compiler rewrites operator expressions into ordinary function calls: - `a + b` -> `a.plus(b)` - `a - b` -> `a.minus(b)` - `a * b` -> `a.times(b)` - `a[i]` -> `a.get(i)` - `a[i] = v` -> `a.set(i, v)` - `a()` -> `a.invoke()` - `a in b` -> `b.contains(a)` (note: receiver is the **right** operand) Because the binding is purely **by name**, there is no marker interface like `Comparable`-style `Addable`; the compiler just resolves the named `operator` function. ## Member or extension An operator function can be a **member** or an **extension**, so you can add operators to types you don't control: ```kotlin operator fun String.times(n: Int) = repeat(n) val line = "ab" * 3 // "ababab" ``` ## Why it exists It makes domain types (money, vectors, matrices, durations) read naturally while keeping everything statically resolved and type-safe.
- Can you create a brand-new operator symbol like `<=>` in Kotlin?No. Kotlin only supports a fixed set of operators; you can give them new meaning but cannot define new symbols or change precedence/associativity.
- What happens if you name a function `plus` but omit `operator`?It compiles as a normal method callable as `a.plus(b)`, but the `a + b` form will not compile because the compiler requires the `operator` modifier.
Operators are nicknames; the operator keyword is the address book entry that tells the compiler whose real (function) name the nickname points to.
saying these in an interview costs you the question
- Thinking you implement an interface (like `Addable`) instead of a named function
- Believing you can invent arbitrary new operator symbols
- Forgetting the `operator` modifier is required
- Saying `a in b` calls `a.contains(b)` (it's `b.contains(a)`)