AHK V2: doc example for "Sub-classing Arrays of Arrays" not working for me

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
SAbboushi
Posts: 252
Joined: 08 Dec 2014, 22:13

AHK V2: doc example for "Sub-classing Arrays of Arrays" not working for me

03 Jul 2018, 01:11

Documentation example for "Sub-classing Arrays of Arrays"

I'm getting following error:
Error: Missing a required parameter.

Specifically: p3

Line#
---> 012: x[p1] := new x.base
Don't know what to make of it...

I've tried

Code: Select all

MsgBox x[1,2]
and

Code: Select all

MsgBox x[1,2,3,4]
to try and make sense of what's going on, but get the same error message...

Can anyone please help?

Code: Select all

x := {base: {addr: Func("x_Addr"), __Set: Func("x_Setter")}}

; Assign value, implicitly calling x_Setter to create sub-objects.
x[1,2,3] := "..."

; Retrieve value and call example method.
MsgBox x[1,2,3] "`n" x.addr() "`n" x[1].addr() "`n" x[1,2].addr()

x_Setter(x, p1, p2, p3) {
    x[p1] := new x.base
}

x_Addr(x) {
    return &x
}
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: AHK V2: doc example for "Sub-classing Arrays of Arrays" not working for me

03 Jul 2018, 01:40

The setter gets called when you assign to an object key that doesn't exist yet.
Assigning something within a setter will also trigger the setter.
And in AHK v2 you need to pass at least the correct amount of variables.

If you combine this knowledge you will find that the code within your setter is the issue. It tries to set a new value that doesn't exist yet which in turn triggers itself.
The problem is though that the key you assign a value to is not long enough. You need at least 3 keys to call the setter correctly from within your setter.

However I think that you don't want to trigger the setter at all. Even if you make your setter call itself correctly the script would still fail.
The Setter would try to assign something which would call a setter which would try to assign something which would call a setter and so on and so on until you get a StackOverflow

You should probably use ObjRawSet to avoid this.

Getters and setters are a pain and I tend to avoid them.
Recommends AHK Studio
SAbboushi
Posts: 252
Joined: 08 Dec 2014, 22:13

Re: AHK V2: doc example for "Sub-classing Arrays of Arrays" not working for me

03 Jul 2018, 06:28

Thanks - hopefully someone will update that example in the AHK V2 documentation

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: Johncoool, jsong55, ntepa and 35 guests