Are there any plans for adding a 'mutableIncrement...
# language-proposals
l
Are there any plans for adding a 'mutableIncrement' operator? Because right now I am not able to write my own extension function for this use case that would behave like this:
Copy code
val foo = AtomicInteger(0)
foo++
Current
increment
operator would allow me to work only with
var foo
, but this is obviously not what I want. In my use case I want to launch N coroutines and show a progress bar with how many of them have already finished (tracked in the
foo
AtomicInteger)
m
Would overriding
add(Integer)
work?
because
++
is basically shorthand for
+= 1
r
Note the docs say (about the
inc
and
dec
operators):
They shouldn't mutate the object on which the
inc
or
dec
was invoked.
https://kotlinlang.org/docs/reference/operator-overloading.html
l
@Matej Drobnič hmmm nope, if you mean plusAssign operator then this works only for
foo += 1
@Ruckus exactly ... so maybe it would be nice to introduce the mutable versions of inc/dec to enable this use case, but it's just a syntactic sugar, not a big deal anyway😊
e
http://kotl.in/issue (there does not seem to be one)
l
Thanks Roman, will check it out and eventually propose there😊
r
@littlelightcz Would it be a different operator? If not, what happens if you override both in the same class?
Or in some sort of class hierarchy?
Copy code
class Thing {
    operator fun inc() { ... }
}
class MutableThing : Thing() {
    operator fun mutableInc() { ... }
}
l
@Ruckus yes, I would propose a brand new operator for this, could be named e.g. mutableInc() and vice versa for decrementing
r
By new operator, I meant a new symbol. If you used the same symbol (
++
) for both
inc
and
mutableInc
, you would have to resolve the conflict I showed above (what would
MutableThing()++
do?). If you also intended a new symbol, did you have one in mind?
e
Please, don’t try to think about this feature in vacuum. There is already a Kotlin design for
a += b
that works both for mutable classes via
a.plusAssign(b)
and for immutable classes via
a = a.plus(b)
. If something in that direction is done for
++
, then it has to have similar design or it is a no-go.
👍 2
r
Ah, fair point. Thanks for the reminder @elizarov
l
Proposal created: https://youtrack.jetbrains.com/issue/KT-31612 , thank you for your inputs.