![Smile :)](./images/smilies/icon_e_smile.gif)
Probably the table gets created already at script start, and dynamic variables get added as we go.
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: If you assign nothing (null) to a variable, it still hasn’t been initialized or “used” in the script yet.
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:var2 := "" ; still never initialized
That's not true, but it appears so because most uninitialized variables have zero capacity.boiler wrote:Credit to @Xtra for noting that address of uninitialized variables are all the same.
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
}
I do not accept your definition of "exists".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)
The variable already exists, as a prerequisite of getting its address.Any non-dynamic reference to a variable creates that variable the moment the script launches.
Source: Functions - Definition & Usage | AutoHotkey
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.gregster wrote:As soon as afunctionvariable gets used (created, but not necessarily assigned), it should show up there.
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.boiler wrote: It actually doesn’t exist as far as AHK is concerned.
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
}
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 insteadjoefiesta wrote:I need IsVar()
then maybe IsVar would be the answer, like IsLabel and IsFunc. But I think it isn't useful to know whether a variable exists.How can I tell if a script has a variable with the given name?
That is EXACTLY how I should have phrased my question."How can I tell if a script has a variable with the given name?"
No, I did.
Users browsing this forum: Bing [Bot], oktavimark and 361 guests