HTD
Kotlin Documentation | Operator overloading 본문
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

'Kotlin' 카테고리의 다른 글
| Kotlin Documentation | Constructing collections (0) | 2023.05.24 |
|---|---|
| Kotlin Documentation | Collections overview (0) | 2023.05.24 |
| Kotlin Documentation | Inline functions (0) | 2023.05.23 |
| Kotlin Documentation | Lamdas (0) | 2023.05.23 |
| Kotlin Documentation | Functions (0) | 2023.05.23 |