Arrays of arrays and the [,,] operator.

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
trismarck
Posts: 506
Joined: 30 Sep 2013, 01:48
Location: Poland

Arrays of arrays and the [,,] operator.

14 Oct 2013, 07:10

Documentation wrote:Multi-dimensional assignments such as table[a, b, c, d] := value are handled as follows:
  • If there is only one key remaining, perform the assignment and return. Otherwise:
  • Search the object for the first key in the list.
  • If a non-object is found, fail.
  • If an object is not found, create one and store it.
  • Recursively invoke the sub-object, passing the remaining keys and value - repeat from the top.

Code: Select all

class baseObj {
	__Get(params*) {
		OutputDebug, "  Get():"
		for each, param in params
			OutputDebug, % "    p[" each "] = " param
		
	}
	__Set(params*) {
		OutputDebug, "  Set():"
		for each, param in params
			OutputDebug, % "    p[" each "] = " param
		ObjInsert(this, params[1], {base: baseObj})
	}
	__Call(params*) {
		OutputDebug, "  Call():"
		for each, param in params
			OutputDebug, % "    p[" each "] = " param
	}
}
;~ a := {base: baseObj}
;~ ;a.b[c] => a["b", c]
;~ a.b := {}
;~ a["b", c] := 4
	;~ ; no meta-function is called

a := {base: baseObj}
;a.b[c] => a["b", c]
a.b := {base: baseObj}
a["b", c] := 4
	; __Set called with two parameters: c and 4
With a["b", c], If a.b contains an object w/o the base, why __Set() is not called, even though, according to the algorithm in the documentation:
  • Recursively invoke the sub-object, passing the remaining keys and value - repeat from the top.
If a.b contains a non-object value, then the recursion stops, but in this case, a.b _contains an object_. If a.b _contains_ an object, shouldn't __Set() be invoked on a.b?
Last edited by trismarck on 08 Jul 2014, 15:27, edited 1 time in total.
lexikos
Posts: 9780
Joined: 30 Sep 2013, 04:07
Contact:

Re: Arrays of arrays and the [,,] operator.

15 Oct 2013, 06:31

If a.b _contains_ an object, shouldn't __Set() be invoked on a.b?
No, and I don't have a clue why you would think otherwise.
Meta-functions are methods defined by an object's base which can define exactly how the object should act when an unknown key is requested.
Source: Objects
"b" is not an unknown key - it has a defined value.

When the sub-object contained by a.b is recursively invoked, a.base no longer has any relevance because a is not the target of invocation.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 90 guests