What I want to avoid is doing something like
Code: Select all
#Warn All, StdOut ; Enable warnings to assist with detecting common errors.
If NOT var
var:="bla"
Note that I am not interested in turning off all such warnings.
TIA
Code: Select all
#Warn All, StdOut ; Enable warnings to assist with detecting common errors.
If NOT var
var:="bla"
Yes, exactly this.
That is exactly what I'm looking for, but yes I meant for v1. Would it be possible/reasonable to silently query the variables list from the script's main window?
Not elegant, but a proof of concept:
Code: Select all
#Warn All, StdOut ; Enable warnings to assist with detecting common errors.
; Check a variable that does not exist, and display the text we're checking against
MsgBox % IsVar("notavar",1)
; Check a variable that does exist
MsgBox % IsVar("A_Args")
; Check if variable exists without displaying a warning (it doesn't)
If !IsVar("notavar")
var:="bla"
ExitApp
IsVar(__var_,__DisplayText_:=0){
DetectHiddenWindows, On
WinMenuSelectItem, ahk_id %A_ScriptHwnd%, , View , 2&
WinHide, ahk_id %A_ScriptHwnd%
WinGetText, __varlist_, ahk_id %A_ScriptHwnd% ;ahk_class Edit1
WinHide, ahk_id %A_ScriptHwnd%
If InStr(__varlist_,"`n" __var_ "[") OR InStr(__varlist_,"`n" __var_ ": Object")
Return 1
If __DisplayText_
MsgBox %__varlist_%
Return 0
}
I forgot about that function, thanks. It makes getting the list of global variables easier. I guess I'd still have to do something inline to get the local variables.gregster wrote: ↑02 Jun 2020, 16:47Perhaps this helps: https://www.autohotkey.com/boards/viewtopic.php?f=6&t=9656
Code: Select all
abc := "Var for abc"
test := "abc"
if isVar(test)
msgbox % """" test """ is a variable & its value is """ isVar(test) """"
else
msgbox variable not found
isVar(testVar){
try
testVar := %testVar%
catch
testVar := 0
return testVar
}
That doesn't quite do it. First of all, the isVar function can be simplified as (the catch block never gets called):icc2icc wrote: ↑23 Jan 2022, 18:52Code: Select all
abc := "Var for abc" test := "abc" if isVar(test) msgbox % """" test """ is a variable & its value is """ isVar(test) """" else msgbox variable not found isVar(testVar){ try testVar := %testVar% catch testVar := 0 return testVar }
Code: Select all
isVar(testVar) {
return %testVar% ? true : false ; Relies on the dynamic variable reference resolving to an existing global variable because no local variable exists by that name.
}
Code: Select all
abc := "Var for abc"
test := "abc"
if isVar(test)
msgbox % """" test """ is a variable & its value is """ %test% """"
else
msgbox variable not found
isVar(testVar) {
return %testVar% ? true : false ; Relies on the dynamic variable reference resolving to an existing global variable because no local variable exists by that name.
}
Code: Select all
foo(3)
foo(x) {
msgbox % isVar(x) ? "The x variable is set" : "The x variable is not set"
}
isVar(testVar){
return %testVar% ? true : false ; Relies on the dynamic variable reference resolving to an existing global variable because no local variable exists by that name.
}
Code: Select all
test1 := 0 ; The variable was set to the value of zero
test2 = "" ; The variable was set to the value of ""
if isVar(test1)
msgbox % "[test1] is a variable & its value is [" test1 "]"
if isVar(test2)
msgbox % "[test2] is a variable & its value is [" test2 "]"
isVar(testVar := ""){
return (testVar <> "")
}
Code: Select all
test := 0 ; The variable test was set to the value of zero
if isVar(test)
msgbox % """" test """ is a variable & its value is """ %test% """"
if !isVar(notVar)
msgbox variable not found
isVar(test := False){
return test
}
In my opinion you didn't. In your earlier slightly edited post, you wrote:
Code: Select all
test2 = "" ; The variable was set to the value of ""
Code: Select all
test2 := """" ; The variable was set to two double-quotes
Code: Select all
test2 := "" ; The variable was set to a blank
What this means is that, at least in AHK v1, by its very existence in a script, a variable is initially set to a blank. The documentation supports that statement:
Thus,https://www.autohotkey.com/docs/Concepts.htm#uninitialised-variables wrote: Although the program automatically initializes all variables (an empty string being the default value), ...
Code: Select all
test1 := ""
MsgBox % isVar(test1) " = " isVar(test2) ; test1 was explicitly set while test2 was automatically set.
A minor point, but just so there's no confusion, the above expression assignment sets the variable to contain one double-quote because each pair of double-quotes inside the quoted string is a single double-quote (because the first one escapes the second one). The following sets it to two:iPhilip wrote: ↑ Because you initialized test2 with the = operator (instead of the := operator - a common error), test2's value is not a blank but two double-quotes. Your statement is equivalent to
Code: Select all
test2 := """" ; The variable was set to two double-quotes
Code: Select all
test2 := """"""
Code: Select all
isVarEmpty(Var){
return Var = ""
}
Yes, but only in AHK v2 via the isSet function.
you are right, I missed the word definedWith respect to the original post:
Because if you send isVar(test) when test was never defined, then its equals to isVar()it's not clear why you have a default value for the input parameter