AutoHotkey Community

It is currently May 27th, 2012, 12:21 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 7 posts ] 
Author Message
PostPosted: November 9th, 2008, 1:40 pm 
Offline

Joined: November 4th, 2008, 9:23 am
Posts: 1045
I wish to add a feature to AHK that resembles Visual Basic's Option Explicit

Visual Basic, like AHK, allows for variables to be introduced "on the fly". However, I personally hate this feature because it makes debugging very difficult when I mistype a variable name. Obviously, if I were perfect, I wouldn't make such mistakes, but sadly I'm not perfect. Now, I think that the default should be "on the fly" variable creation (for backward compatability, and for those that disagree with me). However, I think it would make a nice addition.

Also, for "Arrays", I think only the "base" should need to be declared.

SomeVariable%Index% would have SomeVariable% defined.

Granted, I it could be the case that Index contains "SomeText", and thus SomeVariableSomeText would be the variable.

The idea I'm thinking is you would have a keyword like "Define".

So Define SomeVariable% would be how to define the above variable. The percent sign means that if there is an "addition" like %Index% at that point, then that variable would be defined too. However, if SomeVariableSomeText were called (written as SomeVariableSomeText in the code), then SomeVariableSomeText would have to be defined as well, even though SomeVariable% was already defined.

I believe this prevents limiting of the language, and should minimize typing errors associated with variable usage.

It doesn't limit the variables you can define (because of the % idea, example: SomeVariable% would allow SomeVariable%Index%), and it verifies (at "compile time") that the variables are spelled correctly. Also, by making this opt-in, not only does it allow for backward compatibilty, it allows the programers that don't want this feature to not use it.

Anyfeedback on this idea is much appreciated

_________________
As always, if you have any further questions, don't hesitate to ask.

Add OOP to your scripts via the Class Library. Check out my scripts.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 15th, 2008, 11:44 pm 
Offline

Joined: February 26th, 2006, 9:45 am
Posts: 55
Location: Recife Brazil
I know what you mean; I use to make a lot of type errors.
I personally would like to see AHK staying the way it is,. compact and easy. A very good place to start with programming. Anyone can learn it with it’s detailed help file. Anyway I think AHK can do everything (in rare cases maybe not as fast as you would like to). You could consider writing a script yourself, creating or modifying an editor where you declare/start your variables in the main part. All declared variables you write further in the script would receive a color, red for example. This way it’s easy to discover a wrong typed variable..
When you finish your program, put it on the Scripts\Functions directory so I can use it too hehe..


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 16th, 2008, 12:19 am 
Offline

Joined: November 4th, 2008, 9:23 am
Posts: 1045
lol. I never even thought to do that... I'm going to do that. I'm not sure how well I can do syntax highlighting, but I'll add that in if I can.

The program, upon pressing of the hotkey, read the text in the active window. It will then verify that all variables are defined.

It will (in a message box...) then show the variables that are not defined.

It, of course, will be able to diferentiate local variables from global variables (for functions). Also, it will state that if a given variable has a global part, but not declared global in the function. Example, if in function aFunction, you use aVariable, but aVariable isn't defined locally, but is defined globally, then it will state that in the message box (or file... if I do that).

I'll start on this...shouldn't be TOO hard...

The syntax will be:
;Define variable1, variable2, etc. (for local variables, in fuctions, or global variables, outside functions)
;Define global variable1, variable2, etc. (inside function) to define given values to be defined globally (so if creating a global value in a function, can define it globally)

In other words, the line must contain only the define statement. This way it SHOULDN'T intrerfere with the ability to comment, but at the same time it doesn't become illegal.

Also, the code will verify what is used as the comment marker (in case it was changed).

I'll upload the code when i get a running version. Tell me what I should add to it.


Edit: use a "%" where you plan to use one in the variable. Example, %MyBrowser%_Title%BrowserIndex% and %MyFriendsBrowser%_Title%BrowserIndex% would be %_Title% in the define statement. The program will only check that the format is matched. Of course MyBrowser and MyFriendsBrowser would both have to be defined as well.

_________________
As always, if you have any further questions, don't hesitate to ask.

Add OOP to your scripts via the Class Library. Check out my scripts.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 16th, 2008, 3:19 am 
Offline

Joined: October 17th, 2006, 4:15 pm
Posts: 7503
Location: Australia
This has been mentioned at least twice, probably more:
#MustDeclareVars
#MustDeclareVars or #Strict
Quote:
SomeVariable%Index% would have SomeVariable% defined.
What about more complex dynamic variable references? I think it would be clearer to use *. It should not require the percent signs to match up, so for example Array1_%bc% would be the same as Array%a%_%b%%c% if (bc = b . c) and (a = 1).
Code:
#MustDeclare
Func() {
    local Array*_*
    ;...
}

That's all for arguments sake; it would be more feasible and efficient to implement validation of variable references resolved at load-time, with no validation of dynamic variable references.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 16th, 2008, 5:09 am 
Offline

Joined: November 4th, 2008, 9:23 am
Posts: 1045
Truthfully, I want this protection to be compile-time, not run-time. Thus, I see the two as different.

The first declared Array1_% and the second Array%_%%.

Now, I think you have a good point with the *. So Array1_* and Array*_**. Also, I'm going to allow things such as Array%a%_%b%%c% in the declare (to help understand what a variable is for).

I'm going to consider how to handle the question of "what if one case covers the other". My worry is typos, not how nice the declaration is, but I agree redundancy makes the code worse. I'll put thought into it, and of course, any feedback is welcome.

_________________
As always, if you have any further questions, don't hesitate to ask.

Add OOP to your scripts via the Class Library. Check out my scripts.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 16th, 2008, 6:10 pm 
Offline

Joined: February 26th, 2006, 9:45 am
Posts: 55
Location: Recife Brazil
If you want to share your script (also for AHK starters) %signs may give problems. One off the few confusing things learning AHK are the %sings
Also someone could by acident write to a dereferenced pointer, like %MyBrowser% = 3 It's not in the helpfile but maybe you have read it somewhere in the forum. Anyway see example code below.
Code:
x := 1 ;normal variable declaration
Pointer_x = x ;a pointer to the variable x
MsgBox, x = %x%`nNotice the value of Poiter_x
%Pointer_x% = 2 ;writing to a dereferenced pointer
xTemp := %Pointer_x% ;reading from a dereferenced pointer
MsgBox, x = %xTemp%`nThe value of x was changed by this line:`n`%Poiter_x`% = 2
Also * can be confusing. Like Var*=2 is the same as Var:=Var*2 or also be a dereference giving a numeric memory address
Just my opinion:
If the goal is to discover type errors in script, I would make the program as simple as possible. No need to make difference between global or local. Just start your variables in a comment out block and take the benefit to comment/describe better your vars. (I hate write long variables, but suffered a lot trying to read script after some months, so use to explain them in a block like this)
/* define grColComp ; Green color component
define monToRec ; Money to recieve
*/
Let your script read whole text and find each declared/started variable after string "define". Paint all same (part of)variablestring in the rest of text by pressing hotkey(compile time) or timer(run time). Just verify not colored variables for errors.
Anyway I just want to point to the importance to avoid confusion


Report this post
Top
 Profile  
Reply with quote  
PostPosted: November 2nd, 2009, 7:50 pm 
Offline

Joined: June 20th, 2009, 8:55 pm
Posts: 19
animeaime wrote:
I wish to add a feature to AHK that resembles Visual Basic's Option Explicit


More thoughts about this in the duplicate wish here: http://www.autohotkey.com/forum/viewtopic.php?p=307321


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: No registered users and 21 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