How to Validate if a variable has been initialized

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
gregster
Posts: 8990
Joined: 30 Sep 2013, 06:48

Re: How to Validate if a variable has been initialized

21 Apr 2021, 15:38

Yeah, I already edited that above :)
Probably the table gets created already at script start, and dynamic variables get added as we go.
lexikos
Posts: 9583
Joined: 30 Sep 2013, 04:07
Contact:

Re: How to Validate if a variable has been initialized

22 Apr 2021, 07:28

boiler wrote: If you assign nothing (null) to a variable, it still hasn’t been initialized or “used” in the script yet.
There is no "nothing" or "null" in AutoHotkey. In some other languages, variables are quite often initialized by assigning them a null value. Even if the value you assign is null or an empty string, after any assignment (and before the variable goes out of scope), it cannot be said that the variable is uninitialized.
boiler wrote:var2 := "" ; still never initialized
Incorrect. Also, if you're going to consider a variable which has been explicitly assigned an empty string to be "uninitialized", it would be more consistent to just check for an empty string.
boiler wrote:Credit to @Xtra for noting that address of uninitialized variables are all the same.
That's not true, but it appears so because most uninitialized variables have zero capacity.

Code: Select all

MsgBox % IsInitialized(var) ; not initialized
VarSetCapacity(var, 10000)
MsgBox % IsInitialized(var) ; initialized
var := ""
MsgBox % IsInitialized(var) ; NOT initialized !?

f(0) ; not initialized
f(1) ; initialized
f(0) ; initialized !?

f(b) {
    if b
        v := "Hello"
    MsgBox % IsInitialized(v)
}

IsInitialized(ByRef var) {
	return &var != &UninitializedVar
}
The only way to know whether a variable has previously been assigned a value regardless of its current value is to track that information.

AutoHotkey already does this for the purpose of #Warn UseUnset - which, by the way, would give a warning whenever you call IsInitialized.

If you know that a variable will always have non-zero capacity after it is initialized, you can just use VarSetCapacity(var) = 0. This does not trigger warnings.
boiler wrote: The version that @Xtra and I posted will show if a variable exists (has an actual unique address and has had a value)
I do not accept your definition of "exists".
Any non-dynamic reference to a variable creates that variable the moment the script launches.
Source: Functions - Definition & Usage | AutoHotkey
The variable already exists, as a prerequisite of getting its address.
gregster wrote:As soon as a function variable gets used (created, but not necessarily assigned), it should show up there.
That's exactly why ListVars isn't the answer. Even dynamic variables are created by referencing them, not necessarily by an assignment. If a variable has never been assigned (and is not built-in), it is uninitialized.
boiler wrote: It actually doesn’t exist as far as AHK is concerned.
You now know that all non-dynamic variables exist by the time your code executes, as far as AHK is concerned. If you want to test the conditions under which AHK considers a variable initialized, you need only enable #Warn UseUnset and access the variable.

Code: Select all

#Warn
x := ""
z := x ; no warning
f(1) ; no warning
MsgBox
z := y ; warning
f(0) ; warning

f(a) {
    if a
        b := "Non-zero capacity"
    ; MsgBox % VarSetCapacity(b) ; can be non-zero even when b is uninitialized
    return b
}
joefiesta wrote:I need IsVar()
In a script which contains var := 123 or even if IsInitialized(var), var is obviously a variable. What you need is not IsVar. If the question was instead
How can I tell if a script has a variable with the given name?
then maybe IsVar would be the answer, like IsLabel and IsFunc. But I think it isn't useful to know whether a variable exists.

To determine whether a variable has been initialized, what you need is IsSet.

Spoiler
User avatar
boiler
Posts: 16902
Joined: 21 Dec 2014, 02:44

Re: How to Validate if a variable has been initialized

22 Apr 2021, 07:50

Thanks for the information. I’ll know better than to wade into unknown waters in the future. :D
joefiesta
Posts: 497
Joined: 24 Jan 2016, 13:54
Location: Pa., USA

Re: How to Validate if a variable has been initialized

22 Apr 2021, 09:50

@lexicos said
"How can I tell if a script has a variable with the given name?"
That is EXACTLY how I should have phrased my question.

The problem arose when writing a hotkey subroutine to display variable values (of the same script). I was entering unused variable names and wasn't able to tell they were never used in the script and so the display was not correct. (The fact is, I had overlooked the possibility of entering an unsed variable name.) I want to display or copy to clipboard variable values, since in the subject script they sometimes have very long values, which LISTVARS does not show.
lexikos
Posts: 9583
Joined: 30 Sep 2013, 04:07
Contact:

Re: How to Validate if a variable has been initialized

22 Apr 2021, 22:58

joefiesta wrote:
22 Apr 2021, 09:50
@lexicos said
No, I did.

Anyway...

You're not concerned about whether the variable has ever been assigned a value, only that it is referenced somewhere in the script? I suppose you would want to differentiate between a variable that the script never uses, and one that the script has used or will use.

I suppose that doing a simple word check in the script source would rule out most errors (when you've entered a name that's not used in the script, or made a typo).

With AutoHotkey v2, attempting to retrieve the value of a variable that has no value or does not exist causes an error (which can be caught), so I think this situation would not occur. (Currently creating variables dynamically is not permitted, but if it was, attempting to retrieve the value of a new uninitialized variable would still be an error.)

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 310 guests