Map() clarification?

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
sirksel
Posts: 222
Joined: 12 Nov 2013, 23:48

Map() clarification?

Post by sirksel » 05 Oct 2022, 10:25

I'm trying to decide if I need to guard against unset in the enumeration of a Map. If there is an unset value in the parameter array passed to the Map instantiator, what happens? For example:

Code: Select all

Map('a',10, 'b',, 'c',30)
It appears that the 'b' key is never set, rather than it being set to unset. The setting of the 'b' key seems to fail silently, which isn't necessarily a bad thing. Is that a correct assessment of what's happening there? I couldn't find this case documented anywhere in the docs, but I might have missed it. Is this behavior likely to change? If not, I guess I don't have to guard against unset values in a Map.

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

Re: Map() clarification?

Post by lexikos » 05 Oct 2022, 22:57

What does "set to unset" mean? "Unset" is not a value.

It's either set (has() a value) or not. If it's not set, it's unset.

Map() and .Set() skip pairs that omit the key or value, including something like Map(a, b?) where b is unset. The same applies to {a: b?}. This is intentional and should not change.

Array represents a generally-contiguous sequence, so an index can be out of range or can correspond to an element with no value, with the two conditions being distinct. The Array enumerator iterates over the range (1 to Length), not the present values. Map has no concept of a valid range. A key either has a value associated with it or not.

sirksel
Posts: 222
Joined: 12 Nov 2013, 23:48

Re: Map() clarification?

Post by sirksel » 06 Oct 2022, 06:14

Thanks... that helps a lot. I keep forgetting that unset is more of a condition and not a null-like object. So no guarding against unset values in Maps... keys are either set or they are not! Excellent.

I also appreciate the rationale for unset values in the setters of Array vs. Map. That distinction completely makes sense to me now that you explained it. As always, once I understand your thinking behind one of these decisions, it's always very easy to remember the structure of things. I think you've made really great choices about the language. Thanks again for all you have done to make it even better over all these years!

Post Reply

Return to “Ask for Help (v2)”