AutoHotkey Community

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

All times are UTC [ DST ]




Post new topic Reply to topic  [ 38 posts ]  Go to page Previous  1, 2, 3
Author Message
 Post subject: super-global classes
PostPosted: October 1st, 2011, 8:41 pm 
Offline
User avatar

Joined: May 28th, 2011, 9:03 am
Posts: 466
Location: Germany
I don'nt like classes to be super-global by default. So what about:
Code:
Class Dummy { ; not super-global
}

Class Static Dummy { ; not super-global and cannot be overwritten
}

Global Class Dummy { ; super-global
}

Global Class Static Dummy{ ; super-global and cannot be overwritten
}


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 1st, 2011, 8:46 pm 
Offline
User avatar

Joined: May 24th, 2009, 5:35 am
Posts: 2099
Location: Iowa, USA
fragman wrote:
How about making class variables visible only to the new operator?
I would modify this to: have the new keyword access the global scope. Then classes wouldn't have to be super-global.
fragman wrote:
They would still have to exist as global variables though so you can access their static properties.
I'm not sure what you mean by this. You could declare the class variable global if need be.

_________________
Image
Recommended: AutoHotkey_L
Basic Webpage Controls


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 2nd, 2011, 1:11 am 
Offline

Joined: October 17th, 2006, 4:15 pm
Posts: 7503
Location: Australia
jethrow wrote:
fragman wrote:
How about making class variables visible only to the new operator?
I would modify this to: have the new keyword access the global scope. Then classes wouldn't have to be super-global.
If the new operator has different behaviour to every other expression with that class name, it introduces inconsistency and makes it difficult to get through the concept that a class name is just a global variable. So when I originally thought of resolving the class name at load time (i.e. in order to avoid the need for a global declaration), I decided it was better to require the declaration (and to allow changes to the class variables to take effect).

just me wrote:
I don'nt like classes to be super-global by default.
Well, I do, and those users that have posted in Ask for Help when they were unable to get classes working surely would as well.
Quote:
Class Static Dummy { ; not super-global and cannot be overwritten
}
I plan to add an optional warning for re-assigning class variables, enabled by default.

Tuncay <mobile> wrote:
I can see coming that many users will be against super globals.
Do you have some information that I don't?

From the confused users I've seen in Ask for Help, it seems to me that AutoHotkey's unconventional variable scoping rules are not intuitive even to beginners. Users with experience in other languages should expect "global" variables to actually be global, as is logical and conventional, not require redeclaration in each scope. So I think super-globals will be very welcome, though apparently a love-hate sort of feature if you're right.

I believe the reasoning behind AutoHotkey's variable scoping is:
  • Variable declarations aren't required.
  • Global variables are often used (without declaration) in global subroutines, especially hotkeys.
  • Variables used in subroutines are often logically (but not actually) local to that subroutine, so shouldn't be automatically visible in functions.
The implication is that you should probably be using local variables; but with the current design, it is often inconvenient. On that note, block-scoped variables would be a better solution to the problem (pseudo-code follows):
Code:
^+o:: ; Open folder of file in editor.
{
    local title, path
    WinGetTitle title, A
    RegExMatch(title, ".*?(?=\\[^\\]+ [-*] SciTE)", path)
    Run %path%
}


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 2nd, 2011, 9:08 pm 
Offline
User avatar

Joined: May 28th, 2011, 9:03 am
Posts: 466
Location: Germany
LexiKos wrote:
From the confused users I've seen in Ask for Help, it seems to me that AutoHotkey's unconventional variable scoping rules are not intuitive even to beginners. Users with experience in other languages should expect "global" variables to actually be global, as is logical and conventional, not require redeclaration in each scope.

So why must a variable in "global scope" (i.e. autoexecute-section) be declared as "super-global" using the "keyword" global, when classes don't have to? (I remember "Common source of confusion" in prior help files).


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 2nd, 2011, 10:00 pm 
Offline

Joined: October 17th, 2006, 4:15 pm
Posts: 7503
Location: Australia
See previous post: "I believe the reasoning ..."


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 3rd, 2011, 8:43 pm 
Offline
User avatar

Joined: May 28th, 2011, 9:03 am
Posts: 466
Location: Germany
See previous post:

I don't.


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 3rd, 2011, 10:09 pm 
Offline

Joined: March 27th, 2008, 2:14 pm
Posts: 700
just me:

While classes technically just act like global variables, in practice they are used like functions. One always has access to all functions by name, regardless of scope. Therefore, classes are super-global by default.

Also, as Lexikos has mentioned, variables in subroutines are technically in the global scope, but in practice they are ofttimes treated as local variables. One would not want all of those local-like global variables messing up functions' supposedly local variables unless explicitly stated as such. Therefore, global-space variables are not super-global by default.

Edit: @Tuncay: oh yeah, I forgot to mention, local variables per block sounds like a really cool idea.

_________________
Scripts - License


Last edited by infogulch on October 3rd, 2011, 10:49 pm, edited 1 time in total.

Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 3rd, 2011, 10:15 pm 
Offline

Joined: November 7th, 2006, 9:47 pm
Posts: 1934
Location: Germany
That reminds a wish from me from 2007! >>> Local variables inside blocks (e.g. loop)
Would still like to see it implemented.

_________________
{1:"ahkstdlib", 2:"my libs", 3:"my apps", 4:"my license"}
--> Don't feed the troll! <--


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 38 posts ]  Go to page Previous  1, 2, 3

All times are UTC [ DST ]


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot 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