Page 1 of 2

PB 6.0 and the new map behaviour

Posted: Thu Sep 15, 2022 1:06 pm
by infratec
The following code works in 5.73 and creates an element in 6.0:

Code: Select all

Procedure.i GetValue(Map TestMap.i(), Key$)
  ProcedureReturn TestMap(Key$)
EndProcedure


NewMap Test.i()

Debug MapSize(Test())
Debug GetValue(Test(), "test")
Debug MapSize(Test())
PB 6.0:
0
0
1
PB 5.73:
0
0
0
Just tested with same result:

Code: Select all

NewMap Test.i()

Debug MapSize(Test())
Debug Test("test")
Debug MapSize(Test())
I don't think that creating a map element is wanted in this case.

It breaks many of my codes. I just fixed one with an additional FindMapElement().

Tested with PB x86 on Win10.



// Moved from "Bugs - Windows" to "General Discussion" (Kiffi)
// see also: viewtopic.php?t=79835

Re: PB 6.0 and the new map behaviour

Posted: Thu Sep 15, 2022 4:50 pm
by Andre
I don't know if there was a "wanted" change to the map behavior with PB6.00, but we had a similar topic already last year: viewtopic.php?p=576259

Re: PB 6.0 and the new map behaviour

Posted: Thu Sep 15, 2022 5:43 pm
by skywalk
Fred said this was fixed in v6? :(
News wrote:- Changed: the way the map elements are created when using passive syntax, to be more consistent

Re: PB 6.0 and the new map behaviour

Posted: Thu Sep 15, 2022 8:34 pm
by STARGÅTE
This is no bug, it was a wanted change.
And yes, the bug (that sometimes no element was created and some times an element was added) is now fixed (more consistent).
Now, every access to a map element, creates an element.

If this solution is the "intuitive concept" could be discussed, of cause.
I only know, that in previous versions of PB, there was some trouble with "ghost" or "dummy" map elements, when reading structured maps like MyMap("noExistingKey")\String or LinkedList ect.

Re: PB 6.0 and the new map behaviour

Posted: Thu Sep 15, 2022 8:47 pm
by infratec
But then the help is not on the latest changes because in the (german) help for AddMapElement() you can still read:
Diese Funktion ist nicht zwingend beim Umgang mit Maps, da Elemente automatisch hinzugefügt werden, wenn ihnen ein Wert zugewiesen wird.
This means that an element is only created when a value is assigned to the element.

English version:
This function isn't mandatory when dealing with maps, as elements are automatically added when affecting a value to them.
At least there is a bug:
in the functionality or in the documentation.

I think we need Fred for a decision.

Re: PB 6.0 and the new map behaviour

Posted: Thu Sep 15, 2022 9:11 pm
by jacdelad
@infratec: ??? But both quotes say the same thing, in German AND English. Both say you don't need AddMapElement() and just need to assign a value. But you still have the option to add an element with AddMapElement().

Beside that: I find it super confusing, that an element is added by assigning a value. In my opinion it should only be added with AddMapElement() (like AddElement() for lists). How do other languages handle that? I never worked with maps outside of purebasic.

Re: PB 6.0 and the new map behaviour

Posted: Thu Sep 15, 2022 9:22 pm
by STARGÅTE
infratec wrote: Thu Sep 15, 2022 8:47 pm But then the help is not on the latest changes because in the (german) help for AddMapElement() you can still read:
Diese Funktion ist nicht zwingend beim Umgang mit Maps, da Elemente automatisch hinzugefügt werden, wenn ihnen ein Wert zugewiesen wird.
This means that an element is only created when a value is assigned to the element.
No, this is not the meaning!
The meaning is, map elements are also (not only!) added when you assign a value. Here, you do not need an AddElement. The documentation say nothing about the access on non-existing elements.
Probably this is a missing information, which has to addressed in FindMapElement()

Re: PB 6.0 and the new map behaviour

Posted: Thu Sep 15, 2022 9:27 pm
by infratec

Code: Select all

Debug Test("test")
Is not assigning a value to an element.
But in PB 6.0 an element is created.

This is not the behaviour which is written in the help.
And in my opinion the element should not be created.

Ok, the return value can be 0 in two cases:
1. the element is not found
2. the integer element has the value 0

So FindMapElement() is the better solution.

But as written above:
Bug in the documentation
or
Bug in the function

Re: PB 6.0 and the new map behaviour

Posted: Thu Sep 15, 2022 9:35 pm
by Little John
infratec wrote: Thu Sep 15, 2022 9:27 pm

Code: Select all

Debug Test("test")
Is not assigning a value to an element.
But in PB 6.0 an element is created.
This is a bug in my opinion.
But it has nothing got to do with the help for AddElement(). :twisted:

Re: PB 6.0 and the new map behaviour

Posted: Thu Sep 15, 2022 9:36 pm
by jacdelad
STARGÅTE wrote: Thu Sep 15, 2022 9:22 pm
infratec wrote: Thu Sep 15, 2022 8:47 pm But then the help is not on the latest changes because in the (german) help for AddMapElement() you can still read:
Diese Funktion ist nicht zwingend beim Umgang mit Maps, da Elemente automatisch hinzugefügt werden, wenn ihnen ein Wert zugewiesen wird.
This means that an element is only created when a value is assigned to the element.
No, this is not the meaning!
The meaning is, map elements are also (not only!) added when you assign a value. Here, you do not need an AddElement. The documentation say nothing about the access on non-existing elements.
Probably this is a missing information, which has to addressed in FindMapElement()
Ah, I guess I misread the "affecting" in the English help. This means "doing anything with the element, like assigning a value or reading a value" while the German help states "also by assigning a value" and misses out the other cases?!

Re: PB 6.0 and the new map behaviour

Posted: Thu Sep 15, 2022 9:40 pm
by infratec
Little John wrote: Thu Sep 15, 2022 9:35 pm But it has nothing got to do with the help for AddElement(). :twisted:
This is the only place where something is written about adding an element to a map.
So it has to do with the help for AddMapElement()

Or did you really mean AddElement() and it is not a typo?

Re: PB 6.0 and the new map behaviour

Posted: Thu Sep 15, 2022 9:55 pm
by Little John
I was meaning AddMapElement(), sorry.
infratec wrote: Thu Sep 15, 2022 9:40 pm So it has to do with the help for AddMapElement()
No.
You misunderstood the meaning of the help text for AddMapElement() that you quoted (see Stargate's post above).

Re: PB 6.0 and the new map behaviour

Posted: Thu Sep 15, 2022 10:06 pm
by STARGÅTE
infratec wrote: Thu Sep 15, 2022 9:27 pm

Code: Select all

Debug Test("test")
Is not assigning a value to an element.
But in PB 6.0 an element is created.

This is not the behaviour which is written in the help.
And in my opinion the element should not be created.
Please read the introduction for maps: https://www.purebasic.com/documentation ... ewmap.html
Here, it is written (underlined part!):
NewMap wrote: 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.
"accessed or created!"
But, to become the current element is only possible, when the map element exists.
The result is, also accessing creates an element.

Code: Select all

NewMap Test.s()
Debug Test("test")
Test() = "foo bar" ; current element is "test"
Debug Test()
Debug Test("test")

Re: PB 6.0 and the new map behaviour

Posted: Fri Sep 16, 2022 1:39 am
by skywalk
So glad this is clear now. :?: :lol:
I guess I never realized

Code: Select all

If Map("entirely new key")
   ; Do some code
   ; And PB adds a new map element!
   ; But, before v6, not always? 
EndIf

Re: PB 6.0 and the new map behaviour

Posted: Fri Sep 16, 2022 7:25 am
by infratec
Ok.

No problem with this behaviour. If it is documented it is Ok for me.

So FindMapElement() is the only way to go.

But the the german help of AddMapElement() is not correct and should be adjusted.