Incrementing and decrementing operator overloading

There are a lot of things operator overloading can do, and we have used quite a few features here. Check out how we increment a count of online users:

    numOfUsers = numOfUsers.inc()

It is a much more readable code compared to numOfUsers = numOfUsers+1umOfUsers += 1, or numOfUsers++.

Similarly, we can decrement any number by using the dec() function.

Operator overloading applies to the whole set of unary operators, increment-decrement operators, binary operators, and index access operator. Read more about all of them at https://kotlinlang.org/docs/reference/operator-overloading.html.

Get Kotlin Blueprints 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.