February 2018
Intermediate to advanced
350 pages
7h 35m
English
Binary operators receive a parameter (there are exceptions to this rule—invoke and indexed access).
The Pack.plus extension function receives a Wolf parameter and returns a new Pack. Note that MutableMap also has a plus (+) operator:
operator fun Pack.plus(wolf: Wolf) = Pack(this.members.toMutableMap() + (wolf.name to wolf))val biggerPack = northPack + Wolf("Bad Wolf")
The following table will show you all the possible binary operators that can be overloaded:
|
Operator |
Equivalent | Notes |
| x + y | x.plus(y) | |
| x - y | x.minus(y) | |
| x * y | x.times(y) | |
| x / y | x.div(y) | |
| x % y | x.rem(y) |
From Kotlin 1.1, previously mod. |
| x..y | x.rangeTo(y) | |
| x in y | y.contains(x) | |
| x !in y | !y.contains(x) | |
| x += y | x.plussAssign(y) | Must return ... |
Read now
Unlock full access