# Operator overloading Kotlin allows you to provide custom implementations for the predefined set of operators on types. These operators have predefined symbolic representation (like `+` or `*`) and precedence. To implement an operator, provide a [member function](functions.html#member-functions) or an [extension function](extensions.html) with a specific name for the corresponding type. This type becomes the left-hand side type for binary operations and the argument type for the unary ones. To overload an operator, mark the corresponding function with the `operator` modifier: ```KOTLIN interface IndexedContainer { operator fun get(index: Int) } ``` When [overriding](inheritance.html#overriding-methods) your operator overloads, you can omit `operator`: ```KOTLIN class OrdersList: IndexedContainer { override fun get(index: Int) { /*...*/ } } ``` ## Unary operations ### Unary prefix operators | Expression |Translated to | ----------------------------- | `+a` |`a.unaryPlus()` | | `-a` |`a.unaryMinus()` | | `!a` |`a.not()` | This table says that when the compiler processes, for example, an expression `+a`, it performs the following steps: * Determines the type of `a`, let it be `T`. * Looks up a function `unaryPlus()` with the `operator` modifier and no parameters for the receiver `T`, that means a member function or an extension function. * If the function is absent or ambiguous, it is a compilation error. * If the function is present and its return type is `R`, the expression `+a` has type `R`. Note: These operations, as well as all the others, are optimized for [basic types](types-overview.html) and do not introduce overhead of function calls for them. As an example, here's how you can overload the unary minus operator: ```KOTLIN data class Point(val x: Int, val y: Int) operator fun Point.unaryMinus() = Point(-x, -y) val point = Point(10, 20) fun main() { println(-point) // prints "Point(x=-10, y=-20)" } ``` ### Increments and decrements | Expression |Translated to | ----------------------------- | `a++` |`a.inc()` + see below | | `a--` |`a.dec()` + see below | The `inc()` and `dec()` functions must return a value, which will be assigned to the variable on which the `++` or `--` operation was used. They shouldn't mutate the object on which the `inc` or `dec` was invoked. The compiler performs the following steps for resolution of an operator in the postfix form, for example `a++`: * Determines the type of `a`, let it be `T`. * Looks up a function `inc()` with the `operator` modifier and no parameters, applicable to the receiver of type `T`. * Checks that the return type of the function is a subtype of `T`. The effect of computing the expression is: * Store the initial value of `a` to a temporary storage `a0`. * Assign the result of `a0.inc()` to `a`. * Return `a0` as the result of the expression. For `a--` the steps are completely analogous. For the prefix forms `++a` and `--a` resolution works the same way, and the effect is: * Assign the result of `a.inc()` to `a`. * Return the new value of `a` as a result of the expression. ## Binary operations ### Arithmetic operators | Expression |Translated to | ----------------------------- | `a + b` |`a.plus(b)` | | `a - b` |`a.minus(b)` | | `a * b` |`a.times(b)` | | `a / b` |`a.div(b)` | | `a % b` |`a.rem(b)` | | `a..b` |`a.rangeTo(b)` | | `a.. b` |`a.compareTo(b) > 0` | | `a < b` |`a.compareTo(b) < 0` | | `a >= b` |`a.compareTo(b) >= 0` | | `a <= b` |`a.compareTo(b) <= 0` | All comparisons are translated into calls to `compareTo`, that is required to return `Int`. ### Property delegation operators `provideDelegate`, `getValue` and `setValue` operator functions are described in [Delegated properties](delegated-properties.html). ## Infix calls for named functions You can simulate custom infix operations by using [infix function calls](functions.html#infix-notation).