Operator Overloading
In this page:
class ClassName(val value: Type) {
operator fun plus(other: ClassName): ClassName = ClassName(/* combine */)
operator fun get(index: Int): Type = value
override fun equals(other: Any?): Boolean = /* compare */
}
a + b
a[0]
Overloading the Plus Operator
Defining an operator fun plus(other: Type) inside a class lets instances of that class be combined with the + symbol directly.
Example: Overloading the Plus Operator
// Define a data class `Point`
data class Point(val x: Int, val y: Int) {
operator fun plus(other: Point): Point = Point(x + other.x, y + other.y)
}
// Entry point: execution of the program starts here
fun main() {
// Declare a read-only variable `p1`, set to `Point(1, 2)`
val p1 = Point(1, 2)
// Declare a read-only variable `p2`, set to `Point(3, 4)`
val p2 = Point(3, 4)
// Print `p1 + p2` to the console, with a trailing newline
println(p1 + p2)
}
Login to try C/C++/Java/PHP code in the editor
Overloading Comparison with equals
Overriding equals() (which data classes do automatically) defines what == means for a type, letting two separate instances be considered logically equal based on their content.
Example: Overloading Comparison with equals
// Define a class `Money`
class Money(val cents: Int) {
override fun equals(other: Any?): Boolean {
// Return `other is Money && other.cents == cents` from this function
return other is Money && other.cents == cents
}
override fun hashCode(): Int = cents
}
// Entry point: execution of the program starts here
fun main() {
// Declare a read-only variable `a`, set to `Money(500)`
val a = Money(500)
// Declare a read-only variable `b`, set to `Money(500)`
val b = Money(500)
// Print "Equal: ${a == b}" to the console, with a trailing newline
println("Equal: ${a == b}")
}
Login to try C/C++/Java/PHP code in the editor
Overloading Index Access with get
Defining operator fun get(index: Type) lets instances of a class support square-bracket indexing, just like a List or Array.
Example: Overloading Index Access with get
// Define a class `Grid`
class Grid(val values: List<List<Int>>) {
operator fun get(row: Int, col: Int): Int = values[row][col]
}
// Entry point: execution of the program starts here
fun main() {
// Declare a read-only variable `grid`, set to `Grid(listOf(listOf(1, 2), listOf(3, 4)))`
val grid = Grid(listOf(listOf(1, 2), listOf(3, 4)))
// Print "Value at (1, 0): ${grid[1, 0]}" to the console, with a trailing newline
println("Value at (1, 0): ${grid[1, 0]}")
}
Login to try C/C++/Java/PHP code in the editor
Making an Object Callable with invoke
Defining operator fun invoke(...) lets an instance of a class be called directly like a function, using parentheses right after the object's name.
Example: Making an Object Callable with invoke
// Define a class `Greeter`
class Greeter(val greeting: String) {
operator fun invoke(name: String): String = "$greeting, $name!"
}
// Entry point: execution of the program starts here
fun main() {
// Declare a read-only variable `greeter`, set to `Greeter("Hello")`
val greeter = Greeter("Hello")
// Print `greeter("Kotlin")` to the console, with a trailing newline
println(greeter("Kotlin"))
}
Login to try C/C++/Java/PHP code in the editor
- Forgetting the
operatorkeyword before a function named to match an operator convention, such asplus, which is required for the overload to actually work. - Overloading an operator in a way that surprises users, such as making
+perform subtraction, which harms readability rather than helping it. - Not matching the exact expected function signature (name and parameter count) for the operator being overloaded.
- Operators like
+,-,*, and==map to specific function names (plus,minus,times,equals) that can be overloaded with theoperatorkeyword. - Overloaded operator functions must be marked
operator funand follow the exact expected signature for that operator. - Overloading
[]access usesget/set, and calling an object like a function usesinvoke. - Operator overloads should behave in a way that matches the operator's normal, expected meaning to keep code readable.
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: