Temporally disable warnings Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
User avatar
RaptorX
Posts: 378
Joined: 06 Dec 2014, 14:27
Contact:

Temporally disable warnings

Post by RaptorX » 15 Aug 2022, 15:45

I am trying to do something that i was able to do in v1 which is setting some variables dynamically:

Code: Select all

for title in ["Director","Owner","Officer"]
	%title% := a_index

Msgbox officer
obviously as those variables are not declared anywhere v2 shows an error.

Is there a way to disable errors temporarily as I know that what im trying to do works, i just dont want to be warned about it, but I dont want to turn off all warnings for the whole script. Is just that particular section.

I guess the one way to do it is this:

Code: Select all

Director := Owner := Officer := ""
for title in ["Director","Owner","Officer"]
	%title% := a_index

Msgbox officer
But that would be an issue if there are many way to many variables or if the variable list gets created dynamically.
Any pointers on this would be great.
Projects:
AHK-ToolKit

swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Temporally disable warnings

Post by swagfag » 15 Aug 2022, 19:28

create an Object/Map, stuff its properties/keys with the "variables'" values

abandon any development ideas of using dynamic variables in v2

Rohwedder
Posts: 7626
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Temporally disable warnings

Post by Rohwedder » 16 Aug 2022, 09:25

Hallo,
try:

Code: Select all

#Requires AutoHotkey v2.0-
Fixable(Err, Mode) {
Return -(Mode="Return")
}#Warn All, Off

OnError(Fixable) ;fixable Errors will be fixed
for title in ["Director","Owner","Officer"]
	%title% := a_index
OnError(Fixable, 0) ;fixable Errors are shown

Msgbox officer

Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Temporally disable warnings

Post by Helgef » 19 Aug 2022, 09:13

@Rohwedder, you cannot create variables dynamically in v2. Your code does not solve this, there exists no variables called Director or Owner at any time during the execution of your script.

Cheers

Rohwedder
Posts: 7626
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Temporally disable warnings

Post by Rohwedder » 19 Aug 2022, 10:43

It is enough for me if they simulate their existence sufficiently.

Code: Select all

#Requires AutoHotkey v2.0-
Fixable(Err, Mode) {
Return -(Mode="Return")
}#Warn All, Off

OnError(Fixable) ;fixable Errors will be fixed
for title in ["Director","Owner","Officer"]
	%title% := a_index
OnError(Fixable, 0) ;fixable Errors are shown
ToolTip Director " > " Owner " > " Officer
ListVars
1 > 2 > 3
Global Variables (alphabetical)
--------------------------------------------------
A_Args: Array object {address: 0xA366E0}
Director: 1
Officer: 3
Owner: 2
title[0 of 0]:

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

Re: Temporally disable warnings

Post by lexikos » 20 Aug 2022, 22:46

@Rohwedder OnError serves no purpose in your example. Due to the non-dynamic references in your script, the variables already exist and therefore no errors are thrown. Without those non-dynamic references, suppressing the error would only suppress the error; the assignment would still fail.

Code: Select all

#Requires AutoHotkey v2.0-beta.7
#Warn VarUnset, Off
for title in ["Director","Owner","Officer"]
	%title% := a_index
ToolTip Director " > " Owner " > " Officer
ListVars

Rohwedder
Posts: 7626
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Temporally disable warnings

Post by Rohwedder » 21 Aug 2022, 03:44

I have found the corresponding reference in the manual:
https://lexikos.github.io/v2/docs/Variables.htm#operators
Variables cannot be created dynamically, but a variable can be assigned dynamically if it has been declared or referenced non-dynamically somewhere in the script.
If this was the price for DoNotUseArray%n+1% and %(%triple%)% are valid, then it was too high.

User avatar
RaptorX
Posts: 378
Joined: 06 Dec 2014, 14:27
Contact:

Re: Temporally disable warnings

Post by RaptorX » 21 Aug 2022, 09:53

Rohwedder wrote:
21 Aug 2022, 03:44
If this was the price for DoNotUseArray%n+1% and %(%triple%)% are valid, then it was too high.
It is a high price indeed, but I think there might be some other reasons.
Projects:
AHK-ToolKit

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

Re: Temporally disable warnings  Topic is solved

Post by lexikos » 21 Aug 2022, 16:57

That has nothing to do with it. It was removed to make the rules for variable scope understandable, avoiding inconsistency like:

Code: Select all

fa() {
    MsgBox x  ; error (OK)
    %'x'% := 1  ; create local x
    MsgBox x  ; 1
}

y := "global"
fb() {
    MsgBox y  ; global
    %'y'% := 1  ; create local y (assigning a global should require declaration or assume-global)
    MsgBox y  ; still global!
}
I have yet to see a compelling example demonstrating the value of dynamically creating variables. Sometimes I think experience with dynamic variables distracts v1 users from superior solutions. One example.

More examples.

Code: Select all

for title in [&Director, &Owner, &Officer]
	%title% := a_index

Msgbox officer

Code: Select all

roles := {} ; (You might find a more meaningful name.)
for title in ["Director","Owner","Officer"]
	roles.%title% := a_index

Msgbox roles.officer
Neither of these examples conflict with the (very valuable) VarUnset warning.

Related:
viewtopic.php?p=386685#p386685

User avatar
RaptorX
Posts: 378
Joined: 06 Dec 2014, 14:27
Contact:

Re: Temporally disable warnings

Post by RaptorX » 22 Aug 2022, 17:40

lexikos wrote:
21 Aug 2022, 16:57
I have yet to see a compelling example demonstrating the value of dynamically creating variables.
Well, for me it is a quick way to emulate enums...

Code: Select all

; instead of this:
Director := 1
Officer := 2
Owner := 3
Subordinate := 4

; i do a two liner:
for title in ["Director", "Officer", "Owner", "Subordinate"]
	%title% := a_index
And is flexible enough if I need to add a variable between officer and owner and dont want to modify the other 20 variables i had already written :P

In other situations I am getting data from external sources (i do webscraping very often) and find myself trying to organize the data in a meaningful way based on the info I received, and resort to looping and creating certain types of variables on the fly.

of course, my situations are basically edge cases, and Im fine with autohotkey not handling those things optimally but i just found it suprising that i couldnt do it in v2
lexikos wrote:
21 Aug 2022, 16:57
More examples.

Code: Select all

for title in [&Director, &Owner, &Officer]
	%title% := a_index

Msgbox officer
I think this is just perfect or what I need, Never thought that just "mentioning" the variable as a varRef would do the trick.
Projects:
AHK-ToolKit

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

Re: Temporally disable warnings

Post by lexikos » 22 Aug 2022, 19:41

Never thought that just "mentioning" the variable as a varRef would do the trick.
What do you think is happening?

&Director takes a reference to the Director variable; i.e. a VarRef. The VarRef is stored in the array and subsequently assigned to title. %title% can be used to dereference either by name or by reference.

User avatar
RaptorX
Posts: 378
Joined: 06 Dec 2014, 14:27
Contact:

Re: Temporally disable warnings

Post by RaptorX » 23 Aug 2022, 08:33

I think I understand how it works, but I have mainly used VarRefs in DllCalls and never occurred to me that I could use it in this context.
Some times, even if you understand some concepts you need to SEE it done in a particular concept for it to click in your mind :)
%title% can be used to dereference either by name or by reference.
I think this is the part that i didnt think was possible until you mentioned in your example.
Projects:
AHK-ToolKit

Post Reply

Return to “Ask for Help (v2)”