Rohwedder wrote: ↑03 Mar 2024, 03:40
The (pre-)result (which the MsgBox has to display here) of the post-incrementation of a variable is the value of the variable
before the incrementation, i.e.
the untouched old content!
In the case of a number, exactly this number, in the case of a string, exactly this string.
(But no problem, even bugs have a right to exist.)
I do not understand what is so difficult to understand about this simple sequence of events: first the increment is evaluated, then the MsgBox command is called. There is no such thing as "untouched old content" or "retroactive effect". At the moment the MsgBox command uses its parameter, there is only the current (new) value of the variable, and the result of the expression. The command uses the result of the expression, which as I've pointed out multiple times, is blank due to the non-numeric value, as documented.
Code: Select all
N = 0
--N++
MsgBox,% N ; 1 , i.e. --N++ works
MsgBox,% --N++ ; "" should be 0
Regardless of the order, if both operations were performed on the variable, it should have the same numeric value that it had before the operation, because it is being incremented by 1 and decremented by 1, or vice versa. Yet the first MsgBox shows 1, meaning that the variable was only incremented and not decremented. In other words, "i.e. --N++ works" is a mistake. It does not work. In the standalone
--N++, you are never using the final result of the post-increment.
Post-
++ is evaluated first, and it does not produce a variable, so pre-
-- cannot be evaluated. This is by design.
// DUE TO CODE SIZE AND PERFORMANCE decided not to support things like the following:
...
// -> --Var++ ; Fails because ++ has higher precedence than --, but it produces an operand that isn't
// a variable, so the "--" fails. Things like --Var++ seem pointless anyway because they seem
// nearly identical to the sub-expression (Var+1)? Anyway, --Var++ could probably be supported
// using the loop described in the previous example.
Source: AutoHotkey/source/script_expression.cpp at v1.1.37.01 · AutoHotkey/AutoHotkey · GitHub
lmstearn wrote: ↑05 Mar 2024, 01:54
Even though it is a standalone statement, it should produce a syntax error, as the docs (and the Standard) indicate they are exclusive constructs:
I think there are three errors in your statement:
- It is not a standalone increment statement. If the line was ++N or N++, the increment would be "alone on a line". Two increments together is not "alone".
- The program literally cannot produce a syntax error; the author produces a syntax error, and (execution of a script containing) the syntax error produces a bad result or an error dialog. Expression syntax errors in v1 generally do not produce an error dialog, which is what I assume you actually meant by "produce a syntax error". So if this syntax is invalid, the result should be blank in v1 - and the result is blank, even if the syntax isn't technically invalid. In v2, it produces a load-time error dialog.
- An example not covering a scenario says nothing about whether the scenario is valid or supported. The remaining part of your quote only describes the effect of post-increment, and does not indicate whether pre- and post- operators can or cannot be combined.
(--N)++ is valid and works. If N was originally 0, it sets N to 0 but returns -1. You would only see -1 if you directly used the expression, as in
MsgBox % (--N)++.