static initialization, function auto-execution? Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
User avatar
TheDewd
Posts: 1513
Joined: 19 Dec 2013, 11:16
Location: USA

static initialization, function auto-execution?

Post by TheDewd » 27 Mar 2024, 10:35

In v1, I was able to use the code below to automatically execute code inside a function without needed to call it from within the auto-execute section.

The code would be executed when the script runs, without first needing to manually call it.

Is this functionality removed from v2? Is there still a way to achieve this without adding MyFunction() to the top of my script?

Code: Select all

MyFunction() {
	Static Init := MyFunction() ; Call function
	
	; Code to be executed
}

To further clarify what I'm trying to achieve -- here's an example by Lexikos showcasing this hack: viewtopic.php?p=23235#p23235
lexikos wrote:
06 Aug 2014, 03:20

Code: Select all

init_this_file() {
    static _ := init_this_file()
    /*
    This function is called automatically when the script starts,
    regardless of where it is #included.
    */
}

User avatar
mikeyww
Posts: 26973
Joined: 09 Sep 2014, 18:38

Re: static initialization, function auto-execution?

Post by mikeyww » 27 Mar 2024, 11:15

Hello,

I think it is answered below.
Static/class variable initializers are now executed within the context of a static __Init method, so this refers to the class and the initializers can create local variables. They are evaluated when the class is referenced for the first time (rather than being evaluated before the auto-execute section begins, strictly in the order of definition). If the class is not referenced sooner, they are evaluated when the class definition is reached during execution, so initialization of global variables can occur first, without putting them into a class.
Source: Changes from v1.1 to v2.0 | AutoHotkey v2
EDIT: corrected by lexikos below.
Last edited by mikeyww on 28 Mar 2024, 06:54, edited 1 time in total.

lexikos
Posts: 9592
Joined: 30 Sep 2013, 04:07
Contact:

Re: static initialization, function auto-execution?  Topic is solved

Post by lexikos » 28 Mar 2024, 03:00

@mikeyww that is only about initialization of classes and property initializers in a class. It does not relate to static variables in a function, although the differences from v1 (and relation to initialization of global variables mentioned in the quote) are similar.

The use of static to auto-execute a function is directly addressed under "Variables" in that same document.
Local static variables are initialized if and when execution reaches them, instead of being executed in linear order before the auto-execute section begins. Each initializer has no effect the second time it is reached. Multiple declarations are permitted and may execute for the same variable at different times. There are multiple benefits:
  • When a static initializer calls other functions with static variables, there is less risk of initializers having not executed yet due to the order of the function definitions.
  • Because the function has been called, parameters, A_ThisFunc and closures are available (they previously were not).
  • A static variable can be initialized conditionally, adding flexibility, while still only executing once without requiring if IsSet().
  • Since there may be multiple initializers for a single static variable, compound assignments such as static x += 1 are permitted. (This change reduced code size marginally as it was already permitted by local and global.)
Note: static init := somefunction() can no longer be used to auto-execute somefunction. However, since label-and-return based subroutines can now be completely avoided, the auto-execute section is able to span the entire script.
Source: Changes from v1.1 to v2.0 | AutoHotkey v2
I wouldn't call the old behaviour "functionality". If you want the file to be self-contained, you can place the function call in the same file as the definition - above the start of the function definition instead of below it. If you want the call to occur at a particular point in the startup sequence of the script, you can place the #include accordingly. In v1 this was further complicated by static initializers being taken out of sequence with the rest of the script, while still being strictly limited to the sequence they are defined.

User avatar
mikeyww
Posts: 26973
Joined: 09 Sep 2014, 18:38

Re: static initialization, function auto-execution?

Post by mikeyww » 28 Mar 2024, 06:53

I stand corrected! :)

Post Reply

Return to “Ask for Help (v2)”