[2.0-beta.11] Map __Enum not finding item set with Default Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
Descolada
Posts: 1099
Joined: 23 Dec 2021, 02:30

[2.0-beta.11] Map __Enum not finding item set with Default

Post by Descolada » 05 Oct 2022, 13:04

[Moderator's note: Topic moved from Bug Reports.]

Code: Select all

test := Map()
test.Default := []
test["a"].Push(1)
test["b"] := []
test["b"].Push(2)
MsgBox("a: " test["a"][1] "`nb: " test["b"][1])
for k, v in test
    MsgBox("key: " k "`nvalue: " v[1])
Only two MsgBox show with this script (expected three), but the first MsgBox shows that there should be at least 2 items to enumerate.

safetycar
Posts: 435
Joined: 12 Aug 2017, 04:27

Re: [2.0-beta.11] Map __Enum not finding item set with Default

Post by safetycar » 05 Oct 2022, 13:57

It seems like a complicated case.
But leaving aside the creation of the entry with Push, notice that other new keys with different name are going to add to the same default array instead of creating a new one.
Example:

Code: Select all

test := Map()
test.Default := []
test["a"].Push(1)
test["b"].Push(2)
msgbox test["a"][1] test["a"][2] ; outputs 12 (notice the same key)

While I was writing I just remembered something I tried the other day, it could be helpful for you.
It is a simple concept, it's possible that some more thought can be put into it:

Code: Select all

class MapWithArrayDefault extends Map {
	default[*] => []
	add(val) => this[val] := this[val]
}

mymap := MapWithArrayDefault()
mymap.add("a").push(1)
mymap.add("b").push(2)
msgbox mymap["a"][1] ; 1
msgbox mymap["b"][1] ; 2

Descolada
Posts: 1099
Joined: 23 Dec 2021, 02:30

Re: [2.0-beta.11] Map __Enum not finding item set with Default

Post by Descolada » 05 Oct 2022, 14:35

@safetycar, indeed test.Default := [] will return a pointer to the same array whenever the default value is fetched. I believe this is because objects aren't copied by default but instead the pointer is passed:

Code: Select all

test := Map()
other := test
test["a"] := 1
MsgBox(other["a"])
Perhaps this issue could be subverted if __Item checked whether MapObj.Default contains an object and used obj.Clone() before returning the value? I too haven't found a good workaround yet.

But this should be unrelated to my original issue.

lexikos
Posts: 9553
Joined: 30 Sep 2013, 04:07
Contact:

Re: [2.0-beta.11] Map __Enum not finding item set with Default  Topic is solved

Post by lexikos » 05 Oct 2022, 20:13

Items aren't set with Default. I think you're misunderstanding what it does.

Descolada
Posts: 1099
Joined: 23 Dec 2021, 02:30

Re: [2.0-beta.11] Map __Enum not finding item set with Default

Post by Descolada » 05 Oct 2022, 22:55

@lexikos, ah, I see what you mean. Default just returns a value if no key exists, but doesn't set the requested key to have the Default value. No bug to report indeed.

Post Reply

Return to “Ask for Help (v2)”