Modify

Opened 6 years ago

#3767 new Feature Request

Defer keyword

Reported by: matwachich@… Owned by:
Milestone: Component: AutoIt
Version: Severity: None
Keywords: defer keyword Cc:

Description

Why not a Defer keyword for the AutoIt language?
Just like https://golangbot.com/defer/

Code is much more clear and clean when Close/Terminate/Deinit/Destroy is just below the corresponding Open/Startup/Init/Create.
And lesser memory leaks (handle not closed...).

Func readFile($sFile)
    Local $hF = FileOpen($sFile)
    Defer FileClose($hF)

    Return FileRead($sFile)
EndFunc

Func doStuff()
    Local $hDC = _WinAPI_GetDC()
    Local $hFont = _WinAPI_CreateFont()
    Local $hOld = _WinAPI_SelectObject($hDC, $hFont)
    Defer
        _WinAPI_SelectObject($hDC, $hOld)
        _WinAPI_DestroyObject($hFont)
        _WinAPI_ReleaseDC($hDC)
    EndDefer

    ... do stuff, and don't care about ressources freeing ...
EndFunc

; OR

Func doStuff()
    Local $hDC = _WinAPI_GetDC()
    Defer _WinAPI_ReleaseDC($hDC)

    Local $hFont = _WinAPI_CreateFont()
    Defer _WinAPI_DestroyObject($hFont)

    Local $hOld = _WinAPI_SelectObject($hDC, $hFont)
    Defer _WinAPI_SelectObject($hDC, $hOld)

    ... do stuff, and don't care about ressources freeing ...
EndFunc

Attachments (0)

Change History (0)

Modify Ticket

Action
as new The ticket will remain with no owner.

Add Comment


E-mail address and name can be saved in the Preferences .
 
Note: See TracTickets for help on using tickets.