AutoHotkey Community

It is currently May 27th, 2012, 11:44 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 22 posts ]  Go to page 1, 2  Next
Author Message
PostPosted: May 27th, 2010, 7:05 am 
Offline

Joined: September 23rd, 2009, 6:33 pm
Posts: 19
Many, many times I had the same type of bugs while programming in ahk: I forgot to declare a global variable used in a function. Autohotkey would not warn me. It just would create a blank local variable instead. Of course, the code would not work properly; so I had to spend time debugging it.

I suggest a compiler error/warning; when a local variable has the same name as a global one. If a local variable name matches to a global variable name; the programmer most likely meant using the global variable and just forgot to declare it global.


Last edited by Andrej on May 27th, 2010, 8:24 am, edited 1 time in total.

Report this post
Top
 Profile  
Reply with quote  
PostPosted: May 27th, 2010, 8:08 am 
Offline
User avatar

Joined: December 26th, 2005, 4:40 pm
Posts: 8776
Andrej wrote:
I suggest a compiler error/warning; when a local variable has the same name as a global one.


Practically impossible, I guess.
The are hundreds of 'user library' functions available in the forum and many of them might get into 'standard library'.
For an average user, it would be very hard to look into every function before he can use them in global scope.


Report this post
Top
 Profile  
Reply with quote  
PostPosted: May 27th, 2010, 8:19 am 
Offline

Joined: September 23rd, 2009, 6:33 pm
Posts: 19
SKAN wrote:
Practically impossible, I guess.
The are hundreds of 'user library' functions available in the forum and many of them might get into 'standard library'.
For an average user, it would be very hard to look into every function before he can use them in global scope.

I do not think anyone needs to look into every library function. One just uses any libraries they need and, if they had the warning due to an occasional match of their global variable name to a library function's local variable name; they would simply rename their global variable. Since there should not be many global variables in a program; it should not be a big problem


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 27th, 2010, 8:54 am 
Offline

Joined: May 27th, 2007, 9:41 am
Posts: 4999
I don't think it is even possible. What about something like this:

global variable

Quote:
index10:="Hi there"


and in a function you have a loop that creates variables

Code:
func(loopit)
{
 loop % loopit
  index%A_Index%:=Hi
}


the script doesn't know the value of loopit which could be the result of another function or user input so before running the script you will never be able to tell if it will create a local index10

_________________
AHK FAQ
TF : Text files & strings lib, TF Forum


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 27th, 2010, 12:05 pm 
Offline

Joined: September 23rd, 2009, 6:33 pm
Posts: 19
hugov wrote:
the script doesn't know the value of loopit which could be the result of another function or user input so before running the script you will never be able to tell if it will create a local index10

Well, instead of a compiler warning; it could be a run-time error or it could be two checks: both at compile and run time. Autohotkey works the same way when it checks the correctness of a variable name: it is done during compiling/parsing; but when a variable name is calculated at run time and the name is wrong; you could get an error at run time:

Code:
v1 := "wrong var name"
v2 := %v1% ; run time error


Last edited by Andrej on May 27th, 2010, 12:55 pm, edited 1 time in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 27th, 2010, 12:20 pm 
Offline

Joined: May 27th, 2007, 9:41 am
Posts: 4999
Andrej wrote:
Well, instead of a compiler warning; it could be a run-time error or it could be check both at compile and run time. Autohotkey works the same way when it checks the correctness of a variable name: it is done during compiling/parsing; but when a variable name is calculated at run time and the name is wrong; you could get an error at run time
I don't agree, the code isn't wrong as such, it may unintentional consequences, but it isn't wrong.

_________________
AHK FAQ
TF : Text files & strings lib, TF Forum


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 27th, 2010, 12:47 pm 
Offline

Joined: September 23rd, 2009, 6:33 pm
Posts: 19
hugov wrote:
the code isn't wrong as such, it may unintentional consequences, but it isn't wrong.

Which code is not wrong?
The code I posted produces the following message box at run time.
---------------------------
test.ahk
---------------------------
Error: The following variable name contains an illegal character:
"wrong var name"

The current thread will exit.

Line#
001: v1 := "wrong var name"
---> 002: v2 := %v1%
002: Exit
003: Exit
003: Exit

---------------------------
OK
---------------------------


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 27th, 2010, 12:54 pm 
Offline

Joined: May 27th, 2007, 9:41 am
Posts: 4999
Of course, that is because you made an error :-) I meant this code
Code:
index10:="Hi there" ; global

func(10)
MsgBox % Index10 ; global says hi there

func(loopit)
{
 loop % loopit
  index%A_Index%:="Hi"
MsgBox % Index10 ; local, says Hi
}
nothing wrong with it, may not be what you want or expect but it essence correct code

_________________
AHK FAQ
TF : Text files & strings lib, TF Forum


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 27th, 2010, 1:03 pm 
Offline

Joined: September 23rd, 2009, 6:33 pm
Posts: 19
hugov wrote:
this code
Code:
index10:="Hi there" ; global

func(10)
MsgBox % Index10 ; global says hi there

func(loopit)
{
 loop % loopit
  index%A_Index%:="Hi"
MsgBox % Index10 ; local, says Hi
}
nothing wrong with it, may not be what you want or expect but it essence correct code

You are right. There is nothing wrong with the code. With the warning/error I suggested the code will have to be updated: the global index10 will have to be renamed into g_index10; which only looks like an improvement for me; since it is always better to prefix all global varibales with module name or another prefix


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 27th, 2010, 1:04 pm 
Offline

Joined: May 27th, 2007, 9:41 am
Posts: 4999
Valid:
Code:
v1 := "wrong var name"
v2 = %v1%
v2 := % v1 
v2 := v1
are all valid and could produce different results.

perhaps you should read up on the variables section in the documentation as your error has nothing to do with what you describe as in your first post about global/local variables.

_________________
AHK FAQ
TF : Text files & strings lib, TF Forum


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 27th, 2010, 1:05 pm 
Offline

Joined: May 27th, 2007, 9:41 am
Posts: 4999
Andrej wrote:
With the warning/error I suggested the code will have to be updated: the global index10 will have to be renamed into g_index10; which only looks like an improvement for me; since it is always better to prefix all global varibales with module name or another prefix
that is a matter of personal preference and coding style.

_________________
AHK FAQ
TF : Text files & strings lib, TF Forum


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 27th, 2010, 6:45 pm 
Offline

Joined: April 8th, 2009, 8:23 pm
Posts: 3036
Location: Rio de Janeiro - RJ - Brasil
It's a matter of good coding. Just that.

_________________
"Read the manual. Read it again. Search the forum.
Try something before asking. Show what you've tried.
"
Image
Antonio França
My stuff: Google Profile


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 27th, 2010, 8:31 pm 
Offline
User avatar

Joined: December 21st, 2007, 3:14 pm
Posts: 3826
Location: Louisville KY USA
SKAN wrote:
Have you already tried Lexikos' ListGlobalVars() ?

:)
so with this you can include your libraries and then call this and there by get a starting list of globals and avoid them

_________________
No matter what your oppinion Please join this discussion
Formal request to Polyethene
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 28th, 2010, 2:11 am 
Offline
User avatar

Joined: October 7th, 2006, 8:45 am
Posts: 3330
Location: Simi Valley, CA
I think it would be feasible to write a script that analyzes your code and reports local / global variables with the same name, but to implant a warning in AHK's interpreter for such a warning would instantly break millions of working scripts.

_________________
Ternary (a ? b : c) guide     TSV Table Manipulation Library
Post code inside [code][/code] tags!


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 28th, 2010, 9:29 am 
Offline

Joined: October 17th, 2006, 4:15 pm
Posts: 7503
Location: Australia
Doing this would defeat the point of having separate local and global namespaces.
Code:
func(loopit)
{
 loop % loopit
  index%A_Index%:=Hi
}
In this case there is too much ambiguity for a warning dialog to be displayed.
Quote:
Within a function, any dynamic variable reference such as Array%i% always resolves to a local variable unless no variable of that name exists, in which case a global is used if it exists. If neither exists and the usage requires the variable to be created, it is created as a local variable unless the assume-global mode is in effect.
Source: Functions
It may be that the author intends for the loop to access global variables, or the loop may access global variables by accident instead of creating local variables. There are only two ways there could be conflicting global and local variables:
  • Variables are referenced inside the function non-dynamically; e.g. index1 creates a local variable when the script loads.
  • func is called and creates local variables, then other code creates global variables with the same names, then func is called again. Though local variables are emptied when the function returns, the actual variables will still exist the next time the function is called. Any existing local variable will be used in preference to a global variable with the same name.
It is perfectly valid to have a local variable and global variable with the same name, and preventing this from happening or annoying the user when it does would essentially reduce the functionality of the language.

When a dynamic variable reference is evaluated, the function's local variables are searched before the script's global variables. If a matching local variable is found, it is used and the global variables are not searched. In order to determine if a conflict exists, it would need to search global variables even if a local variable was found.

In short, you're asking for AutoHotkey to complain about what is debatably (not) even an error, at the expense of performance and functionality.
[VxE] wrote:
I think it would be feasible to write a script that analyzes your code and reports local / global variables with the same name,
I agree - I think this sort of thing belongs in an IDE/script editor, not in AutoHotkey itself. Warn the programmer when they write the "bad" code, not when they try to run it.


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 22 posts ]  Go to page 1, 2  Next

All times are UTC [ DST ]


Who is online

Users browsing this forum: Google Feedfetcher and 2 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group