February 2018
Intermediate to advanced
350 pages
7h 35m
English
The indexed access operator is the array read and write operations with square brackets ([]), that is used on languages with C-like syntax. In Kotlin, we use the get operators for reading and set for writing.
With the Pack.get operator, we can use Pack as an array:
operator fun Pack.get(name: String) = members[name]!!val badWolf = biggerPack["Bad Wolf"]
Most of Kotlin data structures have a definition of the get operator, in this case, the Map<K, V> returns a V?.
The following table will show you different declarations of get with a different number of arguments:
| Operator | Equivalent | Notes |
| x[y] | x.get(y) | |
| x[y1, y2] | x.get(y1, y2) | |
| x[y1, y2..., yN] | x.get(y1, y2..., yN) |
The set operator has similar syntax:
enum class ...Read now
Unlock full access