Operator Overloading

You have seen that Kotlin’s built-in types come with a range of available operations and that some types tailor those operations based on the data they represent. Take the equals function and its associated == operator: You can use them to check whether two instances of a numeric type have the same value, whether two strings hold the same sequence of characters, and whether instances of a data class have the same values for properties in the primary constructor. Similarly, the plus function and + operator add two numeric values together, append one string to the end of another, and add the elements of one list to another.

When you create your own types, the Kotlin compiler does not automatically know how to apply ...

Get Kotlin Programming: The Big Nerd Ranch Guide, First Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.