It is helpful to be able to evaluate code before run-time.
This is normally achieved with internal (e.g. Lisp-like macro) or external code generation.
In AutoHotkey, you can achieve something similar by assigning to a static variable inside a procedure or a global variable inside the auto-execute section.
Code: Select all
GLOBAL_VAR := StrSplit("foo, bar, baz, quxx", [", "])
ExampleFunc()
{
local
static LocalVar := StrSplit("foo, bar, baz, quxx", [", "])
return LocalVar
}
Although this might seem obvious, its uses might not.
I had been planning to use this technique to parse human readable strings representing a function’s signature into a data structure to be used for error reporting. Avoiding repeatedly converting the string into a data structure would have allowed the code to be easy to understand while still being efficient. Abandoning the project is one reason I decided to write about this trick. I did not want it to be lost.
It is also useful for making debugging easier. Most types cannot be converted to strings in AutoHotkey, so I frequently have to resort to single-stepping through a procedure to understand what is going wrong. If you are certain a problem is not in a particular expression and you can statically evaluate it, which requires it to be a constant expression, then assigning it to a static variable will keep you from having to single-step through that part of the code repeatedly.