operator overloading

Kotlin protip: indexed access operator overloading with multiple indices

Posted on by  
Riccardo Lippolis

Operator overloading is one of Kotlin’s most beloved features. It lets you give familiar syntax to custom types, and you’re probably already using it without even noticing it. Take the Map interface, for example: the indexed access operator ([]) is used to read and write entries:

val m = mutableMapOf<Int, String>()
m[1] = "one"     // m.put(1, "one")
val value = m[1] // m.get(1)

The same syntax works for arrays, lists, and any type that declares an operator fun get or operator fun set. Did you know that you can overload the indexed access operator with multiple indices?

Continue reading →

shadow-left