guest3456 wrote: ↑02 Dec 2019, 12:13
no. you'd use = for comparison, and you'd use := for assignment
you can assign within an expression, in fact things like this happen all the time:
Code: Select all
if (result := DllCall("asdfad"))
msgbox, result is nonzero: %result%
Code: Select all
if (hwnd := WinExist("Untitled - Notepad"))
I see what you are saying by these examples, but I don't see how the behavior is incorrect usage versus a bug. I always thought this was
intentional behavior programmed by the developers, in AHK v1.
Doing this
if (result := DllCall("asdfad")) is not the same as
If (int1 := "12345678901234567890")
In the example that Pneumatic gave us, he might of wanted to do assignment, but appears to have wrote it incorrectly.
int1 := "12345678901234567890" Without the "if" or Expression.
Or
Code: Select all
int1 := "12345678901234567890"
If (int1 = "12345678901234567890")
{
int2 := int1
msgbox % int1 . "`n" . int2
}
...would have gave him the result desired.
In an expression, the "=" operator, is as if we are saying the variable is equal to or not equal (!=) to. Your use of ":=" in the expression is to assign the
result of the function to the variable, prior to a next step. Using the if (int1 = "12345678901234567890"), is also saying "if true" then... You can also use the ":=" to test if the result of the function is true, after assignment, as well. Which seems confusing, but if we are doing just assignment, we don't need the "if" or to put it in an Expression.
Code: Select all
int1 := "12345678901234567890"
if (int1 := Do())
{
MsgBox,,, True
}
else
{
MsgBox,,, False
}
if (int1 = Do())
{
MsgBox,,, True
}
else
{
MsgBox,,, False
}
if (int1 != Do())
{
MsgBox,,, True
}
else
{
MsgBox,,, False
}
Return
Do()
{
int2 := "12345678901234567890"
Return int2
}
To make the intention of assignment more clear, we could have removed the "if" or took it out of an Expression, and just had
result := DllCall("asdfad") or
int1 := Do(). Because the
result of the function would have changed the contents of the variable, regardless. Putting the assignment into an Expression or with "if", is to save a step, which comes off as a good thing and not a bug. It's just that it can be confusing for those that don't know it's combining steps/code.