https://kotlinlang.org logo
h

Hassaan

09/03/2022, 1:00 PM
As postfix has greater precedence than prefix, then why
Copy code
var num = 0
println(--num + num++) // equals to -2 instead of 0
a

AndreyVanDenHaag

09/03/2022, 1:14 PM
I do not have the answer, but if I see such code during the code review, I ask to change it. I would vote to complete remove all the legacy of C.
👍 2
e

El Anthony

09/03/2022, 1:37 PM
I supose it's because the plus operator evaluates both operands before adding them: (--num) {decrements 'num' to -1, returns -1}, (num++) {returns -1, increments 'num' to 0 }, plus operator { adds -1 and -1, returns -2}
☝️ 2
r

Ruckus

09/03/2022, 3:58 PM
@El Anthony is correct. The relative precedence of the postfix and prefix operators would only come into play for something like
--num++
. Your code has nothing to do with precedence and everything to do with the fact that the
+
operator evaluates its arguments left to right.
h

Hassaan

09/03/2022, 4:06 PM
Yeah cracked it already after a little brainstorming
k

Klitos Kyriacou

09/04/2022, 12:17 PM
Andrey:
I do not have the answer, but if I see such code during the code review, I ask to change it. I would vote to complete remove all the legacy of C.
"All the legacy of C" includes curly brackets and the syntax of while loops. Which parts of C legacy would you remove? The expression
--num + num++
is (and has always been) already specified to result in undefined behaviour in C and therefore most IDEs will warn you about it. Java and Kotlin had decided to change that to be fully specified so they have in fact added further support to that syntax instead of removing it.
a

AndreyVanDenHaag

09/05/2022, 5:07 AM
++ and -- (both in pre and post variant) bring nothing but confusion. If they completely vanish, we will only gain readability (and encourage immutability) (I am sorry I need more time to think about other C legacy.)
k

Klitos Kyriacou

09/05/2022, 8:05 AM
There's also
num += 1
. In C and Java,
++num
means exactly the same thing as `num += 1`: it increments
num
and the result of the expression is its new value. However, Kotlin made
num += 1
a statement but kept
++num
as an expression. Why?
a

AndreyVanDenHaag

09/05/2022, 8:21 AM
Thank you ! This is also a legacy to be removed. Mutable, ugly, unreadable.
m

Michael de Kaste

09/05/2022, 1:40 PM
sometimes mutability is wanted/needed. I prefer a world where vars CAN still be mutable. num++ and num-- being remnants is okay, but its usage should be discouraged, not removed.
a

AndreyVanDenHaag

09/05/2022, 1:52 PM
if you still have this you loose nothing:
num = num + 1
readable, explicit.
k

Klitos Kyriacou

09/05/2022, 3:02 PM
array[expensiveFunction()] = array[expensiveFunction()] + 1
???
a

AndreyVanDenHaag

09/05/2022, 3:10 PM
Sure !!! Exactly to prevent anyone to write something like this:
--array[--expensiveFunction()]++
The code is written for humans:
index = expensiveFunction()
array[index] = array[index] + 1
k

Klitos Kyriacou

09/05/2022, 3:15 PM
That says to me: is
index
used anywhere else? Better check the rest of the function. I would be happy with losing ++ and -- operators, but would find
array[complex_expression] += 1
clearer to read.
6 Views