Page 1 of 1

Map access should not always result in creation of element

Posted: Sun Oct 24, 2021 4:06 am
by Rinzwind
a = mymap("test")
if mymap("test") = 10 ...

These statements should not automatically create an empty key "test" if it does not exist ... This should only happen when mymap is assigned a value (mymap("test") = 10). At the moment we need a combination of findmapelement and return active element to make sure no needless keys are created.

Re: Map access should not always result in creation of element

Posted: Sun Oct 24, 2021 9:02 am
by jacdelad
That's something that hugs me too, but you can circumvent it by using FindMapElement() before accessing it.

Re: Map access should not always result in creation of element

Posted: Sun Oct 24, 2021 9:16 am
by Demivec
Rinzwind wrote: Sun Oct 24, 2021 4:06 am if mymap("test") = 10 ...
Why don't you just use a macro?

Code: Select all

Macro mapTest_mcr(_map_, _key_, _comparison_, _value_)
  (FindMapElement(_map_, _key_) And _map_ _comparison_ _value_)
EndMacro  

If mapTest_mcr(mymap(), "sat", <=, 5)

Re: Map access should not always result in creation of element

Posted: Sun Oct 24, 2021 5:12 pm
by STARGĂ…TE
One can discuss, if an element should always be created when accessing or just when assigning.
The problem at the moment is, that is inconsistent like shown in this example:

Code: Select all

Define NewMap MyMap.s()

Procedure ViewMapElements(Map MyMap.s())
	If MapSize(MyMap())
		ForEach MyMap()
			Debug "   MyMap("+MapKey(MyMap())+") = "+MyMap()
		Next
	Else
		Debug "   Empty"
	EndIf
EndProcedure


Debug "Get Element:"
ClearMap(MyMap())
Define String.s = MyMap("A")  ; No element added
ViewMapElements(MyMap())

Debug "Get Element in Function:"
ClearMap(MyMap())
Define Integer.i = Val(MyMap("A"))  ; Element added
ViewMapElements(MyMap())

Debug "Get Element in Operation:"
ClearMap(MyMap())
Define String.s = MyMap("A") + MyMap("B") ; No element added
ViewMapElements(MyMap())

Debug "Get Element in Function:"
ClearMap(MyMap())
FindString(MyMap("A"), MyMap("B"))  ; Element added
ViewMapElements(MyMap())

Debug "Get Element in If:"  ; Element added
ClearMap(MyMap())
If MyMap("A")
EndIf
ViewMapElements(MyMap())

Debug "Get Element as Pointer:"  ; No element added
ClearMap(MyMap())
Define *Element = @MyMap("A")
ViewMapElements(MyMap())
Further topics:

Re: Map access should not always result in creation of element

Posted: Mon Oct 25, 2021 3:33 am
by Rinzwind
Ah so it's even worse than I thought it was. Fred, action time. Or open source the libraries ;)
I would say, only create on assignment. When comparing/retrieving value, return element when it exists or default value if not (equals empty string or 0). No point in adding empty element, which can quickly add up if you are not aware of it.

Re: Map access should not always result in creation of element

Posted: Mon Oct 25, 2021 4:16 pm
by Andre
We even had a discussion about this topic here:
viewtopic.php?p=519406#p519406

Just remembered that I was the initiator of it back then... :wink:

Re: Map access should not always result in creation of element

Posted: Tue Oct 26, 2021 3:48 am
by Rinzwind
The manual says only assignment creates an element, which is definitely not the case currently. Its wording is somewhat ambiguous btw, "assignment" of what, one assumes to the map.
"When using a new key for an assignment, a new element is automatically added to the map. If another element with the same key is already in the map, it will be replaced by the new one. Once an element as been accessed or created, it becomes the current element of the map, and further access to this element can be done without specify the key. This is useful when using structured map, as no more element lookup is needed to access different structure field."