Hanbit the Developer

Kotlin Documentation | Operator overloading 본문

Mobile/Kotlin

Kotlin Documentation | Operator overloading

hanbikan 2023. 5. 23. 17:37

Kotlin Documentation 시리즈에 대해

Category: Concepts - Functions

문서 링크: https://kotlinlang.org/docs/operator-overloading.html


interface IndexedContainer {
    operator fun get(index: Int)
}
class OrdersList: IndexedContainer {
		// operator modifier is omitted because get() overrides operator fun
    override fun get(index: Int) { /*...*/ }
}

Unary operations

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

Binary operations

Arithmetic operators

in operator

Indexed access operator

invoke operator

Augmented assignments

Equality and inequality operators

 

=== and !== (identity checks) are not overloadable, so no conventions exist for them.

Comparision operators