b+=a not equal to b:=b+a

Share your ideas as to how the documentation can be improved.
tuzi
Posts: 223
Joined: 27 Apr 2016, 23:40

b+=a not equal to b:=b+a

Post by tuzi » 04 Oct 2023, 06:28

[Moderator's note: Topic moved from Bug Reports.]

Code: Select all

a:=""
b:=0
c:=0

b:=b+a
c+=a

MsgBox % b ; b=""
MsgBox % c ; c=0
1.1.37.01 win10x64
geek
Posts: 1055
Joined: 02 Oct 2013, 22:13
Location: GeekDude
Contact:

Re: b+=a not equal to b:=b+a

Post by geek » 04 Oct 2023, 08:19

I don't think bug reports are still being accepted for AutoHotkey v1, but in any case this matches documented intent. Although the two are logically very similar the code b+=a is literally shorthand for EnvAdd b, % a not b := b + a, and per the documentation for EnvAdd:
If either Var or Value is blank or does not start with a number, it is considered to be 0 for the purpose of the calculation (except when used internally in an expression and except when using the TimeUnits parameter).
As opposed to the + operator which documents:
Except where noted below, any blank value (empty string) or non-numeric value involved in a math operation is not assumed to be zero. Instead, it is treated as an error, which causes that part of the expression to evaluate to an empty string. For example, if the variable X is blank, the expression X+1 yields a blank value rather than 1.
Granted, the documentation for + does also say "For example, Var+=2 produces the same result as Var:=Var+2 (though the former performs better)", which as you've seen is only true when working with numbers not empty strings. Maybe we could adjust the documentation to say "(though the former performs better, and results differ when working with empty strings)"
User avatar
JoeWinograd
Posts: 2214
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

Re: b+=a not equal to b:=b+a

Post by JoeWinograd » 04 Oct 2023, 12:11

On the subject of the shorthand operators, both their operation and their documentation, I participated in a thread last year about it. This is the first post in that thread where the issue is discussed:

viewtopic.php?f=76&t=93159&p=484201#p484111

The next five posts after that discuss the issue. My opinion is that the documentation needs improvement. Regards, Joe
Post Reply

Return to “Suggestions on Documentation Improvements”