Does this have side-effects (i.e. on the message r...
# announcements
e
Does this have side-effects (i.e. on the message receiver) or does it simply return an incremented value?
o
Message reciever? You are just incrementing an integer value, what message do you expect to be sent?
e
privet ilya
does my question make sense?
i++ would mutate the receiver
i.inc() does not?
e
Yes I did read this before, many thanks
I think my assumption is correct
i++ will mutate the receiver
a.inc() will not mutate the receiver
i++ != i.inc()
🙂
so, what is the equivalent of i++ for nullable types?
as we can't i?.++
o
what do you want it to mean?
e
I want the equivalent of i?.++ (if it were allowed)
o
Like
i = if (i == null) null else i.inc()
?
e
Perhaps I didn't explain well. I want to be able to express the same semantics as i++ for a potentially nullable i
i++ does not require an assignment obviously, because it mutates i
o
It does
The inc() and dec() functions must return a value, which will be assigned to the variable on which the ++ or -- operation was used.
e
val a : Int = 0 a.inc() a.inc() println(a)
returns 0
o
` ``` `
e
sorry, same even if var declaration
o
I’m not sure what you don’t understand.
i++
translates to
i = i.inc()
, but you provide a different example code. Why should it work like you think it should, if docs tells a different story?
e
sorry yes, you are correct I am looking for something like
Copy code
i = if (i == null) null else i.inc()
I just reread your code
o
I remember you promised to use proper formatting 😉
e
so we need to do the above for
Copy code
i++
?
thanks so much for your help and I apologize for my poor Russian... yah govoroo pa-russki nyet ochen horosho as you can guess
o
Copy code
operator fun Int?.inc() = this?.inc()

fun test() {
    var i: Int? = null
    i++
}
Is it what you’re looking for?
e
exactly!
o
Good, now I can go to bed 😄
e
thanks for all your help ilya much appreciated.. poka 🙂