Kotlin allows operator overloading, which means that we can define our own behavior for operators such as +, -, *, /, %, etc. This can be very useful when working with custom classes that represent abstract concepts such as vectors or matrices, where the typical math operators can be used to simplify the code and make it more readable.
Kotlin provides a set of predefined operators that can be overloaded, and each has a corresponding function that we can define in our custom class to specify the behavior of the operator. These functions have specific names that follow a convention:
| Operator | Function name |
|----------|---------------|
| + | plus() |
| - | minus() |
| * | times() |
| / | div() |
| % | mod() |
| .. | rangeTo()
For example, suppose we have a class called ‘Complex‘ that represents complex numbers. We can define the behavior of the ‘+‘ operator for this class by implementing the ‘plus()‘ function as follows:
class Complex(val real: Double, val imag: Double) {
operator fun plus(other: Complex): Complex {
return Complex(real + other.real, imag + other.imag)
}
}
In this example, we’ve defined the ‘+‘ operator for ‘Complex‘ objects by implementing the ‘plus()‘ function, which takes another ‘Complex‘ object as a parameter and returns a new ‘Complex‘ object whose real and imaginary parts are the sum of the respective parts of the two ‘Complex‘ objects.
Once we’ve defined the ‘plus()‘ function, we can use the ‘+‘ operator on ‘Complex‘ objects like this:
val a = Complex(1.0, 2.0)
val b = Complex(2.0, 3.0)
val c = a + b // c is now Complex(3.0, 5.0)
We can similarly define behavior for other operators as well. For example, we can define the behavior of the ‘-‘ operator as follows:
operator fun minus(other: Complex): Complex {
return Complex(real - other.real, imag - other.imag)
}
In addition to the predefined operators, Kotlin also allows us to define our own custom operators using the ‘operator‘ modifier. However, this should be used sparingly, as it can make the code less readable if overused.
To implement operator overloading for a custom class in Kotlin, we simply define the appropriate operator function as a member function of the class using the ‘operator‘ modifier. We can also define these functions as extension functions if we don’t have access to the source code of the class.
For example, let’s say we have a custom class called ‘Person‘ that represents a person with a name and an age:
class Person(val name: String, val age: Int)
We can define the ‘==‘ operator for this class to compare two ‘Person‘ objects based on their name and age:
operator fun Person.equals(other: Person): Boolean {
return name == other.name && age == other.age
}
With this implementation, we can now use the ‘==‘ operator to compare ‘Person‘ objects:
val person1 = Person("Alice", 25)
val person2 = Person("Alice", 25)
println(person1 == person2) // prints "true", since person1 and person2 have the same name and age
In summary, Kotlin allows us to overload operators to define custom behavior for our own classes. We can do this by defining the appropriate operator function in our class using the ‘operator‘ modifier. By doing so, we can simplify our code and make it more readable when working with custom classes that require mathematical operations or comparisons.