AutoHotkey Community

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

All times are UTC [ DST ]




Post new topic Reply to topic  [ 25 posts ]  Go to page Previous  1, 2
Author Message
 Post subject:
PostPosted: October 20th, 2011, 7:02 pm 
This worked but it's getting way complicated. So I think __Change can simplify it.
Code:
Obj := { "": { base: { property : "value" } }
      , base: { _NewEnum : func("MyEnum")
         , myfunc : func("myfunc")
         , __Set : func("MySet")
         , __Get : func("MyGet") }}

Obj.a := 1   
Obj.a := 2     
Obj.b := 3   

msgbox % Obj.Property 

Obj.myfunc()     
   
For k, v in Obj
   msgbox %k% : %v%
   
MyEnum(this){
   Return this[""]._NewEnum()
}
MySet(this, key, value){
   msgbox % "Function: `t" A_ThisFunc "`nEdited key: `t" key "`nPassed value:`t" value
    return this["",key]:=value
}
MyGet(this, key){
   return this["",key]
}
myfunc(this, params*) {
   msgbox % "Function: " A_ThisFunc
}


fragman wrote:
I'm not quite sure why it's not getting the property from the base object though.
It's because it reads Obj[""].base instead of Obj.base.
Quote:
Btw: Why not use class syntax?
I don't have much of a meaning for it. I would go with a simpler way.

HotkeyIt, It also works. Thanks.


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: October 20th, 2011, 10:22 pm 
Offline

Joined: October 13th, 2009, 10:09 pm
Posts: 1389
Anonymous wrote:
fragman wrote:
I'm not quite sure why it's not getting the property from the base object though.
It's because it reads Obj[""].base instead of Obj.base.
It should not do that in my example since I specifically exclude base.
Anonymous wrote:
Quote:
Btw: Why not use class syntax?
I don't have much of a meaning for it. I would go with a simpler way.
I actually think that class syntax is much more readable than defining functions manually as keys. Using class syntax, everything that belongs together is confined in a single structure, including functions.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 20th, 2011, 10:44 pm 
fragman wrote:
Anonymous wrote:
fragman wrote:
I'm not quite sure why it's not getting the property from the base object though.
It's because it reads Obj[""].base instead of Obj.base.
It should not do that in my example since I specifically exclude base.
The property isn't included in the base object of Obj[""] and MyGet() was redirecting the reference to Obj[""]. That's why the message box was empty. I can't find a better explanation than that. If you use a debugger, you can see it with Step Into.

Quote:
Anonymous wrote:
Quote:
Btw: Why not use class syntax?
I don't have much of a meaning for it. I would go with a simpler way.
I actually think that class syntax is much more readable than defining functions manually as keys. Using class syntax, everything that belongs together is confined in a single structure, including functions.
If there is a working example that makes me think it is simpler, there is no reason for me to stick with the complex method.


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: October 20th, 2011, 11:24 pm 
Offline

Joined: October 13th, 2009, 10:09 pm
Posts: 1389
You're right, I was thinking that you had stored Property in obj.base instead of obj[""]. Simple class draft with similar functionality, untested though:
Code:
Obj := new CObject()

msgbox % Obj.Property 

Obj.myfunc()     

For k, v in Obj
   msgbox %k% : %v%

class CObject
{
   a := 1
   b := 3
   __New()
   {
      this.Insert("", {Property : "value"}) ;Insert like this to prevent triggering __Set
   }
   __Get(key)
   {
      return this["",key]
   }
   __Set(key, Value)
   {
      msgbox % "Function: `t" A_ThisFunc "`nEdited key: `t" key "`nPassed value:`t" value
      return this["",key]:=value
   }
   _NewEnum()
   { 
      Return this[""]._NewEnum()
   }
   myfunc(params*) {
      msgbox % "Function: " A_ThisFunc
   }
}


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 21st, 2011, 12:45 am 
Code:
obj := new NotifyChange

obj.a := 1
obj.a := 2
obj.b := 3

For k, v in obj
   msgbox %k% : %v%

msgbox % Obj.Property 

Obj.myfunc()     

Return   
   
Class NotifyChange {

    __New(){
        ObjInsert(this,"",{ base: {property : "value"} })
    }
    __Set(key, value){
      msgbox % "Function: `t" A_ThisFunc "`nEdited key: `t" key "`nPassed value:`t" value
        return this["",key]:=value
    }
    __Get(key){
        return this["",key]
    }
   _NewEnum(){
      Return this[""]._NewEnum()
   }
   myfunc(params*) {
      msgbox % "Function: " A_ThisFunc
   }   
}


fragman wrote:
You're right, I was thinking that you had stored Property in obj.base instead of obj[""].
Sounds like you still not getting it but it proves that the workaround is not intelligible and justifies the suggestion.
Quote:
Simple class draft with similar functionality, untested though:
Thanks but your code does not work.


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: October 21st, 2011, 11:57 am 
Offline

Joined: October 13th, 2009, 10:09 pm
Posts: 1389
You're right, it was just a quick draft. I actually tested now and here's a working version:
Code:
Obj := new CObject()

msgbox % Obj.Property 

Obj.myfunc()     

For k, v in Obj
   msgbox %k% : %v%

class CObject
{
   
   __New()
   {
      this.Insert("",{})
      this[""].Insert("Property", "value") ;Insert like this to prevent triggering __Set
      this.a := 1
      this.b := 3
   }
   __Get(key)
   {
      if(key!="")
         return this["",key]
   }
   __Set(key, Value)
   {
      msgbox % "Function: `t" A_ThisFunc "`nEdited key: `t" key "`nPassed value:`t" value
      obj :=
      return this["",key] := Value
   }
   _NewEnum()
   {
      Return this[""]._NewEnum()
   }
   myfunc(params*) {
      msgbox % "Function: " A_ThisFunc
   }
}


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 21st, 2011, 8:31 pm 
You are not seeing the problem, which had been already solved.
I wrote:
the __Get meta function prevents base properties from being retrieved. Any workaround?

See the class example in my previous post; the For-loop does not retrieve Obj.Property but msgbox % Obj.Property does. That's the reason the base object was used.

If we could simply use this.base.haskey(key) in the __Get meta function without infinite recursion, we would have much simpler solution. That's why this suggestion is also important, which you seem not to get it.


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: October 22nd, 2011, 11:25 am 
Offline

Joined: October 13th, 2009, 10:09 pm
Posts: 1389
i just wanted to show an example using class syntax. As i said before you can specifically exclude the base key and proxy object key from __Get to prevent recursion, then you can access it from within __Get without any issues. It's a bit annoying that we need to use proxy objects for tasks like this but handling it is not much of an issue really once it's clear how to do it.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 22nd, 2011, 6:22 pm 
fragman wrote:
As i said before you can specifically exclude the base key and proxy object key from __Get to prevent recursion, then you can access it from within __Get without any issues.
I think you are not seeing the problem but can you show how? FYI, the examples you posted didn't solve the problem.


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: October 22nd, 2011, 8:40 pm 
Offline

Joined: October 13th, 2009, 10:09 pm
Posts: 1389
just make sure not to explicitly return a value if the requested key is "base". If you don't explicitly insert a key named base via objinsert base is handled differently from other keys. I can post an example tomorrow if needed when i get back to a computer.
btw, does the class i posted before not enumerate "property"? i think it does but i can't check right now...


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

All times are UTC [ DST ]


Who is online

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