Last words
What remains to be done?
In a reasonable world: merge the two questions.
In an utopia world: Join all answers in only one question, and select the best.
But not, we are not in that utopia world, not even in that reasonable world.
We are in stackexchange.
Update:
Both answers have been modified. Probably better now. A lot of misleading comments have been removed (well, some, others were put aside). But getting here from there included an edit war, a lot of commenting (a lot of which got erased by high rep users), Many interventions of the mods, strong arm twisting and a discussion on the chat.
All of that would have been avoided if the better (newer) question and answer had been selected as the one to keep, simple.
Is that fair with the questioner and the answer ? Of course it isn't.
Original
This question:
A perfectly valid question, well written and specific to the issue to solve, got classified as a duplicate of:
Which, on a first look seems like an (also) reasonable question of a quite similar concern.
But, looking a bit deeper, it happens that both answers in that second question have troubling (and incorrect) issues and very conflicting comments about the order of evaluation of the ++
:
- The first answer (Archemar) claims that the negation
!
is applied first and then the++
. - Gilles claims that that error is actually correct:
the incrementation is indeed applied after the value of the expression is calculated
- G-Man reinforces that interpretation under the cuonglm answer (the second answer).
the
!
operator is applied to the value ofa[$0]
before it is incremented
- And Gilles re-iterate the same in plain words:
This answer is wrong. The incrementation happens after the result of the ! operator is calculated.
At least, without taking any side, both answers result in confusion and noise.
Nobody has taken the time to address the problem and to make the answers clear, but yet, we are directing new questions to such conflicting answers.
Full disclosure: I do have a position. I believe that both G-man and Gilles are wrong. The only possible (valid) description is that the operator ++
is (as it must) be applied first to a lvalue and, ... later ... the !
operator is applied.
But the real issue to discuss here is: should we naively direct people to older answers that look (on first sight) the same but are fundamentally conflicting and confusing?
Note: this question has been written looking at what those answers look like NOW, they will probably be edited and changed. I, for one, will add another answer to try and address the (several) errors. Of course, after some time has passed to discuss this question.
Question
I hold that we should keep to higher standards than ("first look" or "apparently similar").
What do we think?
!i++
, the increment ofi
happens after the value!i
has been used, so Gilles et al is correct, the increment of the value in the variable happens after the application of!
. You can test this withawk 'BEGIN { i=0; print !i++; print i }'
which prints1
and1
. The first1
is from printing!0
and the second is from printing the incremented value (i
is incremented after the value is used to calculate!i
).(!i)++
could be a valid operation (in any language). So, the++
must be applied first:!(i++)
. @Kusalananda++
operator is applied (both in pre and post increments). The value used to carry on the rest of operations (the rvalue that results from the application of ++) is different in pre-increment and post-increment. In post (the example in the question) the value used for the rest of the expression is the value before the increment. But in any case, the ++ operator must be applied first. The++
operator has two outputs !!. @Kusalananda No comment?++
, I thought you meant the other.!a[$0]++
happens and what values are affected by each. Doing this without adding to the confusion would be good. Adding yet another view in another answer my be confusing, so I would suggest editing the given answer.++
. There is no way in which you could understand that++
has a lower precedence than!
. What gets very confusing is that everyone choose one output of++
and defend his/her position of having chosen the correct one to explain the end result. Again, the operator order is set (in stone) in the operator precedence table. The how that leads to the final result is what needs clarifying (both answers).