Jump to content


Photo

[AHK_L] Class, set base after initialisation of keys


  • Please log in to reply
12 replies to this topic

#1 HotKeyIt

HotKeyIt
  • Fellows
  • 6134 posts

Posted 01 October 2011 - 03:00 PM

Currently this is not possible because base is set before initialization of _base.
It would be great if the keys would be set before base is assigned.

What do you think?
obj:= new _NotifyChange
Class _NotifyChange {
	_base := {}
	__Get(key){
		return this._base[key]
	}
	__Set(key,val){
		this._base[key] := val*2
		Return
	}
}


#2 Lexikos

Lexikos
  • Administrators
  • 8855 posts

Posted 01 October 2011 - 11:05 PM

It's debatable whether the script would want the meta-functions to be skipped. If it really does, it can bypass __Set explicitly:
obj:= new _NotifyChange
Class _NotifyChange {
   [color=red]__New() {
      ObjInsert(this, "_base", {})
   }[/color]
   __Get(key){
      return this._base[key]
   }
   __Set(key,val){
      this._base[key] := val*2
      Return
   }
}
Btw,

Set: If the operation was successful, __Set should return the new value of the field, which may differ from the original r-value. This allows assignments to be chained, as in a.x := b.y := z. An empty string should be returned if the assignment failed, allowing the script to detect the error.

It also makes the code shorter.
__Set(key,val){
      [color=red]return [/color]this._base[key] := val*2
   }


#3 HotKeyIt

HotKeyIt
  • Fellows
  • 6134 posts

Posted 01 October 2011 - 11:26 PM

:oops: forgot about ObjInsert, that is good enough :)
Thanks also for note about return, will consider it next time ;)

#4 just me

just me
  • Members
  • 1175 posts

Posted 02 October 2011 - 07:50 PM

It's debatable whether the script would want the meta-functions to be skipped.

It's not debatable, it shouldn't!

#5 HotKeyIt

HotKeyIt
  • Fellows
  • 6134 posts

Posted 02 October 2011 - 08:23 PM

It's not debatable, it shouldn't!

Possibly we could use local for that, would be great, I think?
Class _NotifyChange {

  [color=red]local _base:={}[/color]

   __Get(key){

      return this._base[key]

   }

   __Set(key,val){

      Return this._base[key] := val*2

   }

}


#6 Lexikos

Lexikos
  • Administrators
  • 8855 posts

Posted 02 October 2011 - 09:12 PM

No. "Local" is used for variable scope. This has nothing to do with scope. The need to bypass the meta-functions should be rare and the current workaround is simple. We don't need a new language element for it.

#7 just me

just me
  • Members
  • 1175 posts

Posted 03 October 2011 - 07:38 PM

@Lexikos:

I'd be glad for a "real world" example, where __Set and __Get methods are provided for class or instance variables?

#8 fragman

fragman
  • Members
  • 1591 posts

Posted 03 October 2011 - 11:53 PM

I have one. I needed to call some functions automatically before the constructor of a class that derives from a base class was called. I used an instance variable in the base class and changed it's set method so that it would not actually set the key. This makes it possible to create base classes which can make sure that their constructor is always called.

#9 just me

just me
  • Members
  • 1175 posts

Posted 10 October 2011 - 07:52 PM

@fragman:

Would you please post it?

#10 fragman

fragman
  • Members
  • 1591 posts

Posted 10 October 2011 - 09:11 PM

See here: <!-- m -->https://github.com/C... ... b/CGUI.ahk<!-- m --> (line 49)

It's used to make sure that the constructor of this class is always called when a class that extends this class is created. The __Set function ignores this specific key so nothing will be stored.

#11 just me

just me
  • Members
  • 1175 posts

Posted 11 October 2011 - 09:53 AM

Sorry for causing confusion, but

It's not debatable, it shouldn't!

should be

..., it shouldn't invoke them!

At time of initialization everything is under control and can be done without the need to invoke the __Set() / __Get() metafunctions. So it makes more sense for me to call the meta-functions only after the initialization part is done.

@fragman:
Probably I'm missing something, but I don't understand why you do what you do with the _Constructor variable.

#12 fragman

fragman
  • Members
  • 1591 posts

Posted 11 October 2011 - 10:44 AM

The variable itself is never assigned. I use it as a means to call the constructor of the CGUI class before the constructor of other classes that extend CGUI is called. I use __Set in CGUI to make sure that no key gets assigned to _Constructor.

#13 Lexikos

Lexikos
  • Administrators
  • 8855 posts

Posted 11 October 2011 - 09:49 PM

just me: I disagree, therefore it is debatable. To not invoke them would be inconsistent with every other assignment. It would also require complicating the design.