Ruckus
01/11/2024, 10:33 PMinterface Slot {
operator fun plus(count: Int): Slot
operator fun minus(count: Int): Slot
}
interface Shelf {
operator fun get(row: Int, col: Int): Slot
operator fun set(row: Int, col: Int, items: Slot)
}
We should be able to do this
shelf[3, 7] += 2
Ruckus
01/11/2024, 10:34 PMChris Lee
01/11/2024, 10:45 PMprivate interface Slot {
operator fun plusAssign(count: Int): Unit
operator fun minus(count: Int): Slot
}
private interface Shelf {
operator fun get(row: Int, col: Int): Slot
operator fun set(row: Int, col: Int, items: Slot)
}
private fun foo() {
val shelf = object: Shelf {
override fun get(row: Int, col: Int): Slot {
TODO()
}
override fun set(row: Int, col: Int, items: Slot) {
TODO()
}
}
val slot = shelf[1, 2]
shelf[1, 2] += 2
shelf[1, 2] = slot - 1
}
Ruckus
01/11/2024, 11:18 PMinterface Shelf {
operator fun get(row: Int, col: Int): Int
operator fun set(row: Int, col: Int, items: Int)
}
shelf[3, 7] += 2 // This works fine
I would just like to see it abstracted out to include the use case I providedAlexis Manin
01/12/2024, 7:26 AMRuckus
01/12/2024, 7:44 PMself[3, 7] += 2
in this example is getting the value, calculating a new value, and setting to the new value. Or did I misunderstand what you meant?Ruckus
01/12/2024, 7:48 PMRuckus
01/12/2024, 7:54 PMget
, plus
, and set
are indeed all called, so why not work with a different addend type so long as the result is the same type?Alexis Manin
01/13/2024, 9:52 AMRuckus
01/13/2024, 4:54 PM+=
means according to the language specification. It is specifically designed to use either plus
or plusAssign
. https://kotlinlang.org/docs/operator-overloading.html#augmented-assignmentsRuckus
01/13/2024, 4:58 PM+=
should only update and not replace would remove the most common use case of increasing a var int:
var x = 7
x += 3
Ruckus
01/17/2024, 10:28 PM