__Set, __Get... don't know if this is intended?

Get help with using AutoHotkey and its commands and hotkeys
User avatar
Relayer
Posts: 138
Joined: 30 Sep 2013, 13:09
Location: Delaware, USA

__Set, __Get... don't know if this is intended?

24 Oct 2019, 13:52

Interesting observation here. I typically put tests in the front end of __Set and __Get to manage what I want them to process or not, otherwise I use the ObjRawSet and ObjRawGet to avoid invoking __Set and __Get. Well, I tripped upon another method of controlling when set and get get called but I'm not sure if it is intended behavior and whether I should rely on it in my script.

This script uses set/get to manage access to an array. As such I pass 3 parameters to __Set and 2 to __Get. By making all parameters required (no default) I find that the functions are not invoked when fewer parameters are "passed". This is the behavior I want in this particular case but it feels like I am violating something.

Relayer

Code: Select all

Class test
{
	__New(m)
	{
		this.m := m
		this[2, 1] := 10
		this.m[2, 3] := 33
	}
	
	__Set(r, c, v) ;no defaulted parameters
	{
		Msgbox % "set`n" . r . "`n" . c . "`n" . v
		Return this.m[r][c] := v
	}
	
	__Get(r, c) ;no defaulted parameters
	{
		Msgbox % "get`n" . r . "`n" . c
		Return this.m[r][c]
	}
}

junk := new test([ [1, 2, 3], [4, 5, 6] ])

; these two statements pass 3 and 2 parameters to __Set / __Get respectively
; __Set / __Get ARE executed in this case
junk[2, 2] := 9
Msgbox % junk[2, 2]

; these two statements pass 2 and 1 parameter(s) to __Set / __Get respectively
; __Set / __Get ARE NOT executed in this case
junk.property := "foo"
Msgbox % junk.nonExistantProperty

ExitApp
Escape::ExitApp
Helgef
Posts: 4464
Joined: 17 Jul 2016, 01:02
Contact:

Re: __Set, __Get... don't know if this is intended?

25 Oct 2019, 03:02

Meta functions wrote: When the script gets, sets or calls a key which does not exist within the target object, the base object is invoked as follows:

If this base object defines the appropriate meta-function, call it.
[...]
You do not define the appropriate meta function, so there is nothing to call.

Also note that,
Meta functions wrote: [...]
If the operation still hasn't been handled,

Get and Call: Return an empty string.
Set: If only one key parameter was given, store the key and value in the target object and return the assigned value.
That is why junk.nonExistantProperty return an empty string and junk.property will return foo.

Cheers.
User avatar
Relayer
Posts: 138
Joined: 30 Sep 2013, 13:09
Location: Delaware, USA

Re: __Set, __Get... don't know if this is intended?

25 Oct 2019, 07:29

Thanks Helgef, that mostly clears things up.

So, can I assume that this is an appropriate way to use the meta functions? In other words, will using them this way be safe long term (in V1... not talking about V2)? Although I kind of tripped on this behavior by accident, it works nicely for what I want to do.

Relayer

Return to “Ask For Help”

Who is online

Users browsing this forum: AHKStudent, boiler, Saarthak Kamra, TAC109 and 31 guests