I think that's just AHK's usual and long-established way of
smartly handling
variables. An
if-comparison with its
= equal-operator causes these strings to be seen as numbers, as long as you don't force it to see them as strings...
https://www.autohotkey.com/docs/Concepts.htm#caching wrote:Caching
Although a variable is typically thought of as holding a single value, and that value having a distinct type (string, number or object), AutoHotkey automatically converts between numbers and strings in cases like
myString + 1 and
MsgBox %myNumber%. As these conversions can happen very frequently, whenever a variable is converted, the result is cached in the variable.
In effect, a variable can contain both a string and a number simultaneously. Usually this just improves the script's performance with no down-sides, but if a variable contains both a number and a string, is it number, or is it a string? This ambiguity causes unexpected behavior in at least two cases:[...]
And it sometimes causes just a bit of confusion
Code: Select all
Var1:="+1"
Var2:="1"
msgbox,% Var1 "`n" Var2 "`nClearly they are different."
if( Var1 "" != Var2 "" )
{
msgbox, % "yes, they are different strings " Var1 " vs. " Var2
msgbox, % "but if you try to calculate with them: " Var1 + Var2 " , " Var1 - Var2
msgbox, % "or do a comparison (for example, but not only with if), you get: " (Var1 = Var2) " (true)"
msgbox, % "now compare strings: " (Var1 "" = Var2 "") " (false)"
}
But...
Edit: I think wolf_II's example suffers from the same caching logic as the original code...
Edit2: Another example
Code: Select all
Var1 :="+1"
msgbox % Var1 + var1
msgbox % Var1 "" + var1