Operator overloading
Operator overloading is a mechanism where programming language operators are implemented in custom, user-defined types. Most of the operators in Kotlin are actually functions. If you define a function in your type named plus
, then you can use the +
operator with the instance of that type. This increases flexibility and allows you to have a type defined in Java, and then use operators instead of functions in Kotlin. Also, thanks to extension functions, this enables adding an operator to existing types that you don't own.
The following table shows how operators map to functions:
Operator | Function |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|