Warning: This variable has not been assigned a value Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
User avatar
Tigerlily
Posts: 377
Joined: 04 Oct 2018, 22:31

Warning: This variable has not been assigned a value

01 Jul 2020, 20:30

I noticed that I started recently getting this error with the some of the latest v2 updates:

Image

I do believe I read somewhere that this is a preliminary step before making it so in later v2 releases this throws a full blown error. I'm just wondering what the proper way to avoid this warning/error-in-later-v2-releases

here is my code snippet which produces this warning.

Code: Select all

;	neededIconsList := ""		
	for iconName, iconURL in iconsNeeded
		neededIconsList .= A_Index "`t- " iconName " icon`n"
I've been using neededIconsList := "" to set the variable to an empty string before filling the var with values, however is there a certain way I'm supposed to be setting this variable specifically for v2?

This didn't set off an error in v1 in this case and I'm curious what is best practice for v2? I'm not sure what the point of this warning is either in this case, any explanation would be nice (*:

thank you
-TL
User avatar
kczx3
Posts: 1648
Joined: 06 Oct 2015, 21:39

Re: Warning: This variable has not been assigned a value

01 Jul 2020, 20:43

In this case, your fix is correct to initially define the variable to an empty string.
lexikos
Posts: 9621
Joined: 30 Sep 2013, 04:07
Contact:

Re: Warning: This variable has not been assigned a value  Topic is solved

06 Jul 2020, 03:18

The warning is that you have not assigned a value, so avoid the warning by assigning a value. #Warn doesn't care what value you assign, but obviously you should assign whichever value you need the variable to have.

Suppose that an uninitialized variable has a random value you cannot predict. What will neededIconsList .= "string" do? You cannot know, unless you replace the unknown value with a known one; i.e. initialize the variable.

Now suppose that you are looking at this code snippet, and do not know whether neededIconsList has previously been assigned a value. How do you know what the variable will contain after the loop executes? The concatenation might not even succeed, such as if neededIconsList contained an Array. Maybe it contains a list already, and this is adding to the list. To know for sure, you may need to search the rest of the script for assignments to neededIconsList.

By contrast, if the variable is explicitly assigned the expected value before the loop, the reader doesn't need to know that it's expected to be empty; they can see that it will be empty. You also don't need to rely on an assumption that the variable is empty, which might turn out to be incorrect.

So it's already best practice to do this, even in v1.

By the way, a v1 script with #Warn will show this same warning.
iseahound
Posts: 1451
Joined: 13 Aug 2016, 21:04
Contact:

Re: Warning: This variable has not been assigned a value

06 Jul 2020, 11:34

As far as I am concerned, assignment is assignment. It should initialize the value for the user automatically.
v2 changes wrote: The operators +=, -=, -- and ++ treat an empty variable as 0 (for += and -=, this only applies to the left-hand side). In v1, this was true only for standalone use of the operator, not when used mid-expression or with multi-statement comma.
lexikos
Posts: 9621
Joined: 30 Sep 2013, 04:07
Contact:

Re: Warning: This variable has not been assigned a value

07 Jul 2020, 22:27

.= isn't assignment. It is concatenation followed by assignment. The concatenation comes first.
User avatar
Tigerlily
Posts: 377
Joined: 04 Oct 2018, 22:31

Re: Warning: This variable has not been assigned a value

08 Jul 2020, 18:44

@lexikos

Thank you for the explanation, makes a lot of sense.

Much appreciated.
-TL

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: songdg and 34 guests