AutoHotkey Community

It is currently May 24th, 2012, 9:20 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 13 posts ] 
Author Message
PostPosted: June 22nd, 2006, 6:48 am 
Offline

Joined: July 20th, 2005, 4:49 am
Posts: 65
It would be really cool if you could tie a variable to a function so that when
a variable is assigned a value or has a it's value retrieved a function is called.

For assignment the return value of the function becomes the value of the variable.

The Tie command might look like this:

Code:
Tie, variablename, functionname


the function is passed a flag whether you are setting or getting the variable. And also the value being assigned to the variable - if setting.

Example:

Code:

Gui,Add,Edit,vEdit1, Dog

Tie, animal, UpdateAnimal

; displays dog
msgbox %animal%

; sets Edit1 contents to Squirrel
animal=squirrel

; manually changed edit1
GuiControl,,Edit1,moose

; pet is now moose
Pet=%animal%

UpdateAnimal(access_type,value)
{
   global Edit1

   if access_type = set
      GuiControl,,Edit1,%value%

   ifaccess_type = get
   {
      GuiSubmit,NoHide
      return Edit1
   }
}



Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 23rd, 2006, 1:39 am 
Offline

Joined: March 2nd, 2004, 3:36 pm
Posts: 10720
I don't see much of a benefit compared to simply calling a function to set the variable and a second function to retrieve it. Both methods provide good "information hiding" to simplify the interface for the caller (your idea may be slightly simpler, but the benefit seems too small to introduce a whole new syntax).

Contrary opinions are welcome.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 23rd, 2006, 8:39 am 
Offline
User avatar

Joined: December 29th, 2004, 1:28 pm
Posts: 2537
I can think of a few uses :) .

One advantage I can see is for creating/using objects if the name of the changed variable could be passed as a param. For example, when using ahkstructlib2 a user wouldn't have to call a function to send a new value to a structure. It could be automatically updated. As they changed the value of one of the local variables the function could automatically be called to update the value in the structure. This usage probably wouldn't be typical though I'm guessing... but has a lot of potential.

Another example could be for error trapping. If a global variable is used that can be modified by more than one method, a function could be used to check if the value goes out of range. It could also be used to test for specific values. Tracking ErrorLevel without having to add multiple If statements throughout code would be handy.

Edit: custom built-in variables could be created if a function could be automatically called when retrieving a variable's value...


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 23rd, 2006, 1:08 pm 
Offline

Joined: July 20th, 2005, 4:49 am
Posts: 65
Chris wrote:
but the benefit seems too small to introduce a whole new syntax).


One of the reasons I suggested this is that there is no change to the existing syntax. Aside of course from the additional command:

Code:
Tie, varname, function


Everything continues to work exactly the same. The command just changes the functionality of variable access, not the syntax used.

You could even implement it like this
Code:
Tie,tiedvar,myfunc

something := tiedvar%othervar%


where the contents of othervar get passed as a parameter to myfunc

Code:
myfunc(access_type,value,arg)


Once again no syntax change, but the meaning of the syntax changes - which I admit is a little concerning but not completely inconsistent with other ahk design decisions.

Another reason for my suggestion is that i figured it could be relatively painless to implement.

This is based on the following possibly wildy erroneous assumptions. :)

1. Variables in ahk have some underlying c++ data structure to represent them which handles their data type, scope and memory requirements.

2. Within that data structure there exists a byte where information is stored about their state using bitmasks. A flag could be added into this byte for whether the variable has been tied or not.

If the flag is not set when the variable is accessed then return it's value as normal. Otherwise look up the tied function in a table of registered tied variables and execute that ahk code passing the return value back as the value of the variable.

If some other implementation exists then it would be unfortunate to have to keep track of which variables had been tied by doing lookups for every single variable access.

Of course my assumptions might be (probably are) misguided. I'm not pushing to have this implemented but I thought it might be a nice idea. My example probably oversimplified the concept, I'll come up with something better if I can.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 23rd, 2006, 1:41 pm 
Offline

Joined: March 2nd, 2004, 3:36 pm
Posts: 10720
It seems to me that this is one of those ideas that has a high coolness factor and looks great in theory. But in practice, it just isn't useful enough to justify the added documentation complexity, let alone the development and code size. In other words, even if the feature were already coded and had almost no impact on code size, I don't know if I'd want it in the help file. This is because I've received some criticisms about the length and complexity of the documentation, which people say makes it hard to learn AutoHotkey and hard to locate the information they need.

Another consideration is that I'm unaware of any other interpreted/scripting languages that have such a construct (though I think quite a few OO programming languages do). This might be because it's such a novel idea that they haven't thought of it. But in my experience, it's far more likely that it's because the idea has poor benefit/cost ratio -- for reasons not fully known to people like me who aren't language-design experts.

On the other hand, it may get implemented someday when I see the merits more clearly, or when someone else decides they want to implement it on their own.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 23rd, 2006, 1:57 pm 
Offline

Joined: December 27th, 2005, 1:46 pm
Posts: 6837
Location: France (near Paris)
Chris wrote:
This is because I've received some criticisms about the length and complexity of the documentation, which people say makes it hard to learn AutoHotkey and hard to locate the information they need.
They never tried to learn Perl or PHP! :-)
Perhaps you should have a "AutoHotkey for dummies" manual, and a more complete one (the current one!).
Just joking, maintaining two sets of documentation is tedious, unless automated.

Chris wrote:
Another consideration is that I'm unaware of any other interpreted/scripting languages that have such a construct
Well, it reminds me of Lua's metadata. It is a powerful feature... that I almost never use. It allows to extend the language and the semantic of data. For example, it eases the implementation of object oriented programming or the handling of user defined data.

I believe it is more urgent to have real array support that such cool feature. :-P

_________________
Image vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 23rd, 2006, 4:41 pm 
Offline
User avatar

Joined: December 29th, 2004, 1:28 pm
Posts: 2537
Another language that has the ability is Envelop (similar to VB4). I can definiteltly see myself using this feature a lot as I have used it in Envelop often. Mostly for creating custom objects though. It does add a lot of flexibility to a language. The description for such a command in a help file wouldn't be too complicated or hard to understand IMO. I would suggest a couple commands. Here's a sample:
Code:
OnChange("VariableName1", "VarHasChanged")
OnQuery("variableName1", "VarValueRequested")

TestVar = 200
MsgBox, %TestVar%  ; was out of range. should get set to 100
MsgBox, %TestVar2% ; could contain the current value specified in an ini file
Return

; call this function if the value of VariableName has changed
; and allow the function to modify the contents if necessary
VarHasChanged(VariableName)
{
  If (VariableName = "TestVar" AND %VariableName%  > 100)
    %VariableName% = 100
}


; call this function if the value of VariableName has been
; requested and allow the function to modify the contents if necessary
VarValueRequested(VariableName)
{
  If (VariableName = "TestVar2") {
    IniRead, OutputVar, Filename, Section, Key
    %VariableName% = %OutputVar%
  }
}

I agree with PhiLho though, it would be great to have but I'd prefer to have other features from the wishlist first ;) .


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 31st, 2007, 7:51 pm 
Offline
User avatar

Joined: December 29th, 2004, 1:28 pm
Posts: 2537
**bump** ..cough...cough..

This functionality would be a great asset when creating custom functions, add-ons for others to use in their scripts. Any chance you might reconsider something like this Chris?

Code:
OnChange("VariableName", "FunctionToCall")
OnQuery("VariableName", "FunctionToCall")


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 31st, 2007, 9:33 pm 
Offline

Joined: March 2nd, 2004, 3:36 pm
Posts: 10720
Although I can see that it'd be useful, I can't see very many people actually using it. Also, it might be somewhat complicated to implement because variables are accessed for both internal and external purposes. Each access spot in the code would have to be reviewed to see if it qualifies as a triggering event.

As an alternative to this idea, consider writing your scripts in object-oriented style. For example, rather than accessing a variable, treat its contents as off-limits for direct access and instead call a function. For example:
Code:
InventoryItem_Read(CarrotSlicer)
...
InventoryItem_Read(ByRef InvItem)
{
    ; ... do your on-access action here...
    return InvItem
}


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 31st, 2007, 10:22 pm 
Offline
User avatar

Joined: December 29th, 2004, 1:28 pm
Posts: 2537
Thanks for the reply :) . I was hoping to use this functionality for creating objects for others to use. The alternative at the moment seems to be to pre-process .ahk code. That is another possibility though...

I realize that this isn't a simple request and I wouldn't want to see something like this add unnecessary delay to v2 release but I think it would make developing objects for use with AutoHotkey code much easier with simpler syntax for users. I realize that this is something that average users probably wouldn't use directly(maybe...), but it would make life easier for those of us that create functions for average users to use/Include in their scripts.

It would simplify syntax in many cases since it would hide much of the syntax necessary to perform various tasks from the average user writing code. Having code automatically executed when a user references a variable allows flexibility when developing add-ons without giving the user additional syntax and extra steps to perform to achieve the functionality. I think that simplifying syntax and minimizing steps for users is very important and can make a big difference to the number of people that are likely to make use of the functionality. This could be a big step in that direction for creating add-ons for users.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 1st, 2007, 4:12 pm 
Offline

Joined: May 24th, 2006, 2:49 pm
Posts: 4511
Location: Belgrade
Quote:
Another consideration is that I'm unaware of any other interpreted/scripting languages that have such a construct

Every object oriented script language, like javascript or lua or perl or python or anything has this. They just call it property.

So usefulness is pretty much out of question.

_________________
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 1st, 2007, 11:58 pm 
Offline
User avatar

Joined: December 29th, 2004, 1:28 pm
Posts: 2537
majkinetor wrote:
So usefulness is pretty much out of question.
Just to clarify, do you mean to say that this functionality being useful is not in question or the opposite?


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 2nd, 2007, 12:42 am 
Offline

Joined: May 24th, 2006, 2:49 pm
Posts: 4511
Location: Belgrade
the first one. :D

_________________
Image


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

All times are UTC [ DST ]


Who is online

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