Page 1 of 1

Arrays of arrays and the [,,] operator.

Posted: 14 Oct 2013, 07:10
by trismarck
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?

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

Posted: 15 Oct 2013, 06:31
by lexikos
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.