AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Tie variable accesses to functions

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Wish List
View previous topic :: View next topic  
Author Message
MisterW



Joined: 20 Jul 2005
Posts: 65

PostPosted: Thu Jun 22, 2006 6:48 am    Post subject: Tie variable accesses to functions Reply with quote

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
   }
}

Back to top
View user's profile Send private message
Chris
Site Admin


Joined: 02 Mar 2004
Posts: 10467

PostPosted: Fri Jun 23, 2006 1:39 am    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message Send e-mail
corrupt



Joined: 29 Dec 2004
Posts: 2391

PostPosted: Fri Jun 23, 2006 8:39 am    Post subject: Reply with quote

I can think of a few uses Smile .

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...
Back to top
View user's profile Send private message Visit poster's website
MisterW



Joined: 20 Jul 2005
Posts: 65

PostPosted: Fri Jun 23, 2006 1:08 pm    Post subject: Reply with quote

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. Smile

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.
Back to top
View user's profile Send private message
Chris
Site Admin


Joined: 02 Mar 2004
Posts: 10467

PostPosted: Fri Jun 23, 2006 1:41 pm    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message Send e-mail
PhiLho



Joined: 27 Dec 2005
Posts: 6721
Location: France (near Paris)

PostPosted: Fri Jun 23, 2006 1:57 pm    Post subject: Reply with quote

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! Smile
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. Razz
_________________
vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")
Back to top
View user's profile Send private message Visit poster's website
corrupt



Joined: 29 Dec 2004
Posts: 2391

PostPosted: Fri Jun 23, 2006 4:41 pm    Post subject: Reply with quote

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 Wink .
Back to top
View user's profile Send private message Visit poster's website
corrupt



Joined: 29 Dec 2004
Posts: 2391

PostPosted: Sat Mar 31, 2007 7:51 pm    Post subject: Reply with quote

**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")
Back to top
View user's profile Send private message Visit poster's website
Chris
Site Admin


Joined: 02 Mar 2004
Posts: 10467

PostPosted: Sat Mar 31, 2007 9:33 pm    Post subject: Reply with quote

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
}
Back to top
View user's profile Send private message Send e-mail
corrupt



Joined: 29 Dec 2004
Posts: 2391

PostPosted: Sat Mar 31, 2007 10:22 pm    Post subject: Reply with quote

Thanks for the reply Smile . 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.
Back to top
View user's profile Send private message Visit poster's website
majkinetor



Joined: 24 May 2006
Posts: 3615
Location: Belgrade

PostPosted: Sun Apr 01, 2007 4:12 pm    Post subject: Reply with quote

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.
_________________
Back to top
View user's profile Send private message MSN Messenger
corrupt



Joined: 29 Dec 2004
Posts: 2391

PostPosted: Sun Apr 01, 2007 11:58 pm    Post subject: Reply with quote

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?
Back to top
View user's profile Send private message Visit poster's website
majkinetor



Joined: 24 May 2006
Posts: 3615
Location: Belgrade

PostPosted: Mon Apr 02, 2007 12:42 am    Post subject: Reply with quote

the first one. Very Happy
_________________
Back to top
View user's profile Send private message MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Wish List All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group