Jump to content

Recommended Posts

Posted

I was looking at implementing some Unit tests so I was looking into VarGetType. I've never realized that functions could be stored in variables before, but I'm not sure what use they have. Printing the function to the console doesn't show anything and it seems that the only thing that can be done with it is call it...

Global $sFunc = Print
$sFunc("Hi")

Func Print($sMsg)
    ConsoleWrite($sMsg & @CRLF)
EndFunc

I can't see how this is useful though, it just seems confusing. Is there something I'm missing?

All my code provided is Public Domain... but it may not work. ;) Use it, change it, break it, whatever you want.

  Reveal hidden contents
Posted

It promotes functions to first-class citizenship. Pretty handy in my view:

Global $AD = (@AutoItVersion = "3.3.14.5" ? _DebugArrayDisplay : _ArrayDisplay)

Global $CW = @Compiled ? __ConsoleWrite : _ConsoleWrite

; if compiled, create a console
Func __ConsoleWrite(ByRef $s)
    Local Static $hCon = __ConsoleInit()
    DllCall("kernel32.dll", "bool", "WriteConsoleW", "handle", $hCon, "wstr", $s & @LF, "dword", StringLen($s) + 1, "dword*", 0, "ptr", 0)
    Return
EndFunc   ;==>__ConsoleWrite

Func __ConsoleInit()
    DllCall("kernel32.dll", "bool", "AllocConsole")
    Return DllCall("kernel32.dll", "handle", "GetStdHandle", "int", -11)[0]
EndFunc   ;==>__ConsoleInit

; if simply run, use Scite's Unicode-aware ConsoleWrite
Func _ConsoleWrite($s)
    ConsoleWrite(BinaryToString(StringToBinary($s & @LF, 4), 1))
EndFunc   ;==>_ConsoleWrite

 

  Reveal hidden contents

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Posted

In my UDF sets, I just check where IsFunc($variable) is used.....

ADO.au3

Func __ADO_EVENT__FetchProgress($iProgress, $iMaxProgress, $i_adStatus, ByRef $oRecordset)
    If Not _ADO_EVENTS_SetUp() Then Return
    __ADO_ConsoleWrite_Blue(" ADO EVENT fired function: __ADO_EVENT__FetchProgress:")
    __ADO_ConsoleWrite_Blue("   $iProgress=" & $iProgress)
    __ADO_ConsoleWrite_Blue("   $iMaxProgress=" & $iMaxProgress)
    __ADO_ConsoleWrite_Blue("   $i_adStatus=" & $i_adStatus)
    If IsFunc($__g_fnFetchProgress) Then
        $__g_fnFetchProgress($iProgress, $iMaxProgress, $i_adStatus, $oRecordset)
    EndIf
    #forceref $oRecordset
EndFunc   ;==>__ADO_EVENT__FetchProgress

...
Func _ADO_COMErrorHandler_UserFunction($fnUserFunction = Default)
    ; in case when user do not set his own function UDF must use internal function to avoid AutoItError
    Local Static $fnUserFunction_Static = ''

    If $fnUserFunction = Default Then
        If Not IsFunc($fnUserFunction) Then Return SetExtended(1, $fnUserFunction_Static)
        ; just return stored static variable
        Return $fnUserFunction_Static
    ElseIf IsFunc($fnUserFunction) Then
        ; set and return static variable
        $fnUserFunction_Static = $fnUserFunction
        Return $fnUserFunction_Static
    Else
        ; reset static variable
        $fnUserFunction_Static = ''
        Return SetError($ADO_ERR_INVALIDPARAMETERTYPE, $ADO_EXT_DEFAULT, $fnUserFunction_Static)
    EndIf
EndFunc   ;==>_ADO_COMErrorHandler_UserFunction

...
Func __ADO_ComErrorHandler_InternalFunction(ByRef $oCOMError)
    ; Do nothing special, just check @error after suspect functions.
    #forceref $oCOMError
    Local $sUserFunction = _ADO_COMErrorHandler_UserFunction()
    If IsFunc($sUserFunction) Then $sUserFunction($oCOMError)
EndFunc   ;==>__ADO_ComErrorHandler_InternalFunction

_GUIRegisterMsgEx.au3

If (@error = $GUIREGISTERMSGEX_CALL_ERROR And @extended = $GUIREGISTERMSGEX_CALL_PARAMS) Then
    If IsFunc($g__fnGUIREGISTERMSGEX_CALLBACK_WRONG_PARAM_ALERT) Then $g__fnGUIREGISTERMSGEX_CALLBACK_WRONG_PARAM_ALERT()
EndIf

ErrorLog.au3

Func _Log_SetOutputFunction($fnFunction = Default)
    Local Static $fnFunction_static = Null
    If $fnFunction = Default And IsFunc($fnFunction_static) Then Return $fnFunction_static
    If Not IsFunc($fnFunction) Then Return SetError(1)

    $fnFunction_static = $fnFunction
    Return $fnFunction_static
EndFunc   ;==>_Log_SetOutputFunction

XML.au3

Func _XML_ComErrorHandler_UserFunction($fnUserFunction = Default)
    ; in case when user do not set his own function UDF must use internal function to avoid AutoItError
    Local Static $fnUserFunction_Static = ''

    If $fnUserFunction = Default Then
        Return $fnUserFunction_Static ; just return stored static variable
    ElseIf IsFunc($fnUserFunction) Then
        $fnUserFunction_Static = $fnUserFunction ; set and return static variable
        Return $fnUserFunction_Static
    EndIf
    $fnUserFunction_Static = '' ; reset static variable

    ; return error as incorrect parameter was passed to this function
    Return SetError($XML_ERR_PARAMETER, $XML_EXT_PARAM1, $fnUserFunction_Static)
EndFunc   ;==>_XML_ComErrorHandler_UserFunction

....

Func __XML_ComErrorHandler_InternalFunction($oCOMError)
    ; If not defined ComErrorHandler_UserFunction then this function do nothing special
    ; In that case you only can check @error / @extended after suspect functions

    Local $sUserFunction = _XML_ComErrorHandler_UserFunction()
    If IsFunc($sUserFunction) Then $sUserFunction($oCOMError)
EndFunc   ;==>__XML_ComErrorHandler_InternalFunction

 

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

Posted (edited)

@seadoggie01 the best explanation / working usage in UDF which show exactly adequate example which is comparable to your snippet is in ErrorLog.au3

.......
Func _Log_SetOutputFunction($fnFunction = Default)
    Local Static $fnFunction_static = Null
    If $fnFunction = Default And IsFunc($fnFunction_static) Then Return $fnFunction_static
    If Not IsFunc($fnFunction) Then Return SetError(1)

    $fnFunction_static = $fnFunction
    Return $fnFunction_static
EndFunc   ;==>_Log_SetOutputFunction
.......
Func __Log_Wrapper($sText)
    Local $fnFunction = _Log_SetOutputFunction()
    If @error Then Return SetError(1)
    $fnFunction($sText)
EndFunc   ;==>__Log_Wrapper
.......

Using _Log_SetOutputFunction()  and _Log() function which use __Log_Wrapper() you can on the fly change the "output device" for _Log() function.

For example I use this following approach in all my projects :

  1. at starts it check if script is compiled and if not then is forwarding to ConsoleWrite()
  2. if is compiled but not entirely initiated "Diagnostic Log window" is not displayed no log is displayed
  3. if the "Diagnostic Log window" is displayed then the log is forwarded to this "Diagnostic Log window" 
  4. if I run compiled script with parameters I can change the log output to file
  5. if I change "Diagnostic Log window"  to use SciLexer.dll then I can ensure you that this will be very easy to forward log to NEW "Diagnostic Log window" 
Edited by mLipok

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

Posted

Thank you all! This is a little mind boggling, but it mostly makes sense now. (Probably more after I sleep on it) Previously I was doing this all with Call and the name of a function in a string if I needed to, but just calling the actual function from a variable is much cleaner. I do wish we could print out their names in the Console.

Is any of this in the help file? I can't find any explanation related to this (unless it is mentioned in examples, which I don't think really counts).

All my code provided is Public Domain... but it may not work. ;) Use it, change it, break it, whatever you want.

  Reveal hidden contents
Posted (edited)
  On 5/28/2020 at 9:28 AM, seadoggie01 said:

(Probably more after I sleep on it)

Expand  

;)

  On 5/28/2020 at 9:28 AM, seadoggie01 said:

I do wish we could print out their names in the Console.

Expand  
_Example()
Func _Example()
    Local $fnTesting = _test
    ConsoleWrite("! FunctionName = " & FuncName($fnTesting) & @CRLF)
EndFunc   ;==>_Example

Func _test()

EndFunc   ;==>_test

 

Edited by mLipok

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

Posted (edited)

FuncName is your friend.

Function-related functions: VarGetType, IsFunc, FuncName

I'm requesting that VarGetType be listed under "Variables and conversion" rather under "Misc" in help file.
Also "Dataypes" fail to list Function type as well as several other native datatypes.

Edited by jchd
  Reveal hidden contents

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Posted

Whaaaa? 😲

Okay, this is useful now. I'm so glad I posted this question.

All my code provided is Public Domain... but it may not work. ;) Use it, change it, break it, whatever you want.

  Reveal hidden contents
Posted
  On 5/28/2020 at 9:28 AM, seadoggie01 said:

Is any of this in the help file? I can't find any explanation related to this (unless it is mentioned in examples, which I don't think really counts).

Expand  

Function Notes

 

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

Posted

Whenever I think, "Okay, there can't be too many more surprises about AutoIt..." this happens. Sheesh

All my code provided is Public Domain... but it may not work. ;) Use it, change it, break it, whatever you want.

  Reveal hidden contents
Posted
  On 5/28/2020 at 9:45 AM, seadoggie01 said:

Whenever I think, "Okay, there can't be too many more surprises about AutoIt..." this happens. Sheesh

Expand  

It only confirms the saying "Don't be so sure" ;)

 

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...