Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Variable Initialization with postfix #319

Closed
wilkster opened this issue Apr 26, 2023 · 7 comments
Closed

Variable Initialization with postfix #319

wilkster opened this issue Apr 26, 2023 · 7 comments

Comments

@wilkster
Copy link

; Below will flag a warning about index not being assigned a value
dummyFunction1() {
someArray := Array()
index++ := someArray.Length
}
; Below works fine
dummyFunction2() {
someArray := Array()
index := someArray.Length
}

Either is valid syntax wise

@RaptorX
Copy link
Contributor

RaptorX commented Apr 27, 2023

Autohotkey v2 will throw a warning with the first function.

You must assign a number before using the ++ operand.

index := 0
Index++ ; all is good
Msgbox index

On the other hand:

index++ ; this will get a warning
Msgbox index

Try it. :)

So the lexer is correctly warning you of the problem.

If it doesn't it is really unexpected for me because everything in the documentation is clear about trying to use empty variables.

@sousvideonlow
Copy link

Yes it seems for good reason that version 2 requires variables to be explicitly declared before we can perform any arithmetic function on them.

@wilkster
Copy link
Author

Being a postfix operator isn't:
index++ := someArray.Length
the same as
index := someArray.Length + 1
or
index := someArray.Length
++index

So it is initialized, then incremented
Seems to work that way in AHK V2 anyhow (C works that way as well as I recall)

@RaptorX
Copy link
Contributor

RaptorX commented Apr 27, 2023

I just tried Your examples on v2 and I do not get a warning when running the code which is highly unexpected.

So you are right, assignment seems to happen before the increment which is very weird looking at how the code is read from left to right 😆

@rebornsick
Copy link

Seems like I have the same issue. Every first variable that WingetPos/MouseGetPos asked get a warning. This showcase from documentation have 5 warnings:

EWD_MoveWindow(*)
{
    CoordMode "Mouse"  ; Switch to screen/absolute coordinates.
    MouseGetPos &EWD_MouseStartX, &EWD_MouseStartY, &EWD_MouseWin
    WinGetPos &EWD_OriginalPosX, &EWD_OriginalPosY,,, EWD_MouseWin
    if !WinGetMinMax(EWD_MouseWin)  ; Only if the window isn't maximized 
        SetTimer EWD_WatchMouse, 10 ; Track the mouse as the user drags it.

    EWD_WatchMouse()
    {
        if !GetKeyState("LButton", "P")  ; Button has been released, so drag is complete.
        {
            SetTimer , 0
            return
        }
        if GetKeyState("Escape", "P")  ; Escape has been pressed, so drag is cancelled.
        {
            SetTimer , 0
            WinMove EWD_OriginalPosX, EWD_OriginalPosY,,, EWD_MouseWin
            return
        }
        ; Otherwise, reposition the window to match the change in mouse coordinates
        ; caused by the user having dragged the mouse:
        CoordMode "Mouse"
        MouseGetPos &EWD_MouseX, &EWD_MouseY
        WinGetPos &EWD_WinX, &EWD_WinY,,, EWD_MouseWin
        SetWinDelay -1   ; Makes the below move faster/smoother.
        WinMove EWD_WinX + EWD_MouseX - EWD_MouseStartX, EWD_WinY + EWD_MouseY - EWD_MouseStartY,,, EWD_MouseWin
        EWD_MouseStartX := EWD_MouseX  ; Update for the next timer-call to this subroutine.
        EWD_MouseStartY := EWD_MouseY
    }
}

Every first "X" like &EWD_MouseStartX &EWD_OriginalPosX &EWD_MouseX &EWD_WinX warned not being assigned a value.

@wilkster
Copy link
Author

@rebornsick Yes, similar issue,
WinGetPos &EWD_WinX, &EWD_WinY, , , EWD_MouseWin
Will flag a warning, where
WinGetPos(&EWD_WinX, &EWD_WinY, , , EWD_MouseWin)
Will not

thqby added a commit that referenced this issue May 4, 2023
@wilkster
Copy link
Author

wilkster commented May 5, 2023

Perfect, thank you!

@thqby thqby closed this as completed May 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants