Increment / Decrement Operator Behavior

Xinok xinok at live.com
Mon Jun 4 13:40:34 PDT 2012


On Monday, 4 June 2012 at 20:08:57 UTC, simendsjo wrote:
> Oh, and what should writeln(i++, ++i, ++i, i++) do?
>
> It is messy whatever the logic implementation.

For prefix operators, it would be logical to perform the action 
before the statement, such as the code would be rewritten as:

++i
++i
writeln(i, i, i, i)
i++
i++

However, I already stated that it wouldn't work for prefix 
operators. Take this statement:

++foo(++i)

There's no way to increment the return value of foo without 
calling foo first. This "logic" would only work for the postfix 
operators.

I came up with the idea after refactoring this code:
https://github.com/Xinok/XSort/blob/master/timsort.d#L111

Each call to mergeAt is followed by --stackLen. I could have used 
stackLen-- in the mergeAt statement instead, but I didn't want to 
rely on operator precedence for the correct behavior.


More information about the Digitalmars-d mailing list