English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Kotlin Operator Overloading

In this article, you will learn about operator overloading through examples (how operators work for user-defined types such as objects).

When using operator in Kotlin, it will call its corresponding member function. For example, the expression a + b is converted to a.plus(b) in the background.

fun main(args: Array<String>) {
    val a = 5
    val b = 10
    print(a.plus(b)) // print(a+b)
}

When running the program, the output is:

15

In fact, the plus() function is overloaded to handle various Kotlin basic types and String (string).

// + operators for basic types
operator fun plus(other: Byte): Int
operator fun plus(other: Short): Int
operator fun plus(other: Int): Int
operator fun plus(other: Long): Long
operator fun plus(other: Float): Float
operator fun plus(other: Double): Double
//for string concatenation
operator fun String?.plus(other: Any?): String

You can also define how operators work on objects by overloading the corresponding functions. For example, you need to overload the plus() function to define+how operators work on objects.

Example: overload+operator

fun main(args: Array<String>) {
    val p1 = Point(3, -8)
    val p2 = Point(2, 9)
    var sum = Point()
    sum = p1 + p2
    println("sum = (${sum.x}, ${sum.y})")
}
class Point(val x: Int = 0, val y: Int = 10) {
    //Overload the plus function
    operator fun plus(p: Point): Point
        return Point(x + p.x, y + p.y)
    }
}

When running the program, the output is:

sum = (5, 1)

Here, the plus() function is marked with the operator keyword to inform the compiler + the operator is overloaded.

expression p1 + p2 is converted to p in the background.1.plus(p2)。

Example:- operator overloading

In this example, you will learn to overload - operator expressions --a is converted to a.dec() in the background.

the dec() member function does not take any parameters.

fun main(args: Array<String>) {
    var point = Point(3, -8)
    --point
    println("point = (${point.x}, ${point.y})")
}
class Point(var x: Int = 0, var y: Int = 10) {
    operator fun dec() = Point(--x, --y)
}

When you run the program, the output will be:

point = (2, -9)

Remember that

operator fun dec() = Point(--x, --y)

is equivalent to

operator fun dec(): Point
    return Point(--x, --y)
}

Note several points

1When overloading operators, it should be attempted to maintain the original function of the operator. For example,

fun main(args: Array<String>) {
    val p1 = Point(3, -8)
    val p2 = Point(2, 9)
    var sum = Point()
    sum = p1 + p2
    println("sum = (${sum.x}, ${sum.y})")
}
class Point(val x: Int = 0, val y: Int = 10) {
    //Overload the plus function
    operator fun plus(p: Point) = Point(x - p.x, y - p.y)
}

Although the above program is technically correct, we use + The operator subtracts the corresponding properties of two objects, making the program confusing.

2Unlike languages such as Scala, Kotlin can only overload a specific set of operators.