Kotlin Operator Overloading - GeeksforGeeks

02 Aug, 2019
Since Kotlin provides user-defined types, it also provides the additional functionality to overload the standard operators, so that working with user-defined types is easier. All of the unary, binary, relational operators can be overloaded. The operators are overloaded either through the member functions or through extension functions. These functions are preceded by the operator modifier. There are standard functions for every type of operator that can be overloaded according to the usage.
The following table shows the various functions that can be defined for unary operators. These functions modify the calling instance.
Here, x corresponds to the type for which the operator is defined. The overloaded functionality is defined within the respective functions.
Kotlin program to demonstrate the unary operator overloading –
class UnaryOverload(var str:String) {    // overloading the function    operator fun unaryMinus() {        str = str.reversed()    }}// main functionfun main(args : Array<String>) {    val obj = UnaryOverload("HELLO")    println("Initial string is ${obj.str}")y    //calling the overloaded function unaryMinus()    -obj    println("String after applying unary operator ${obj.str}")}
Output:
Initial string is HELLO
String after applying unary operator OLLEH

The increment and decrement operator can be defined for a type through the following functions. These function returns a new instance with the outcome of the expression.
Either used in postfix or prefix notation these functions work well in both the cases, with the same expected output, as one would expect when using prefix or postfix notations.
Kotlin program to demonstrate the operator overloading –
class IncDecOverload(var str:String) {    // overloading increment function    operator fun inc(): IncDecOverload {        val obj = IncDecOverload(this.str)        obj.str = obj.str + 'a'        return obj    }    // overloading decrement function    operator fun dec(): IncDecOverload {        val obj = IncDecOverload(this.str)        obj.str = obj.str.substring(0,obj.str.length-1)        return obj    }       override fun toString(): String {        return str    }}// main functionfun main(args: Array<String>) {    var obj = IncDecOverload("Hello")    println(obj++)    println(obj--)    println(++obj)    println(--obj)}
Output:
Hello
Helloa
Helloa
Hello

The following table shows the binary operators and their equivalent functions to be defined. All these functions modify the calling instance.
Kotlin program to overload the plus function –
class Object(var objName: String) {    // Overloading the function    operator fun plus(b: Int) {        objName = "Name is $objName and data is $b"    }    override fun toString(): String {        return objName    }}// main functionfun main() {    val obj = Object("Chair")    // Calling the overloaded function    obj+9    println(obj)}
Output:
Name is Chair and data is 9

Note- The relational operators do not have any specific functions to be defined, to use relational operators on instances of a user-defined type, the type must implement the Comparable interface.
Kotlin supports a wide range of operators, hence defining each for a type is not a good programming practice. The following table shows some of the other useful operators that can be overloaded is Kotlin.
Kotlin
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Broadcast Receiver in Android With Example
Android RecyclerView in Kotlin
Content Providers in Android with Example
Retrofit with Kotlin Coroutine in Android
How to Add and Customize Back Button of Action Bar in Android?
How to Get Current Location in Android?
Kotlin Setters and Getters
How to Change the Color of Status Bar in an Android App?
Kotlin Android Tutorial
Kotlin when expression