PB 6.0 and the new map behaviour

Everything else that doesn't fall into one of the other PB categories.
infratec
Always Here
Always Here
Posts: 7619
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

PB 6.0 and the new map behaviour

Post 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
User avatar
Andre
PureBasic Team
PureBasic Team
Posts: 2139
Joined: Fri Apr 25, 2003 6:14 pm
Location: Germany (Saxony, Deutscheinsiedel)
Contact:

Re: PB 6.0 and the new map behaviour

Post 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
Bye,
...André
(PureBasicTeam::Docs & Support - PureArea.net | Order:: PureBasic | PureVisionXP)
User avatar
skywalk
Addict
Addict
Posts: 4218
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: PB 6.0 and the new map behaviour

Post 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
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
STARGÅTE
Addict
Addict
Posts: 2234
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: PB 6.0 and the new map behaviour

Post 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.
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
infratec
Always Here
Always Here
Posts: 7619
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: PB 6.0 and the new map behaviour

Post 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.
User avatar
jacdelad
Addict
Addict
Posts: 2010
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: PB 6.0 and the new map behaviour

Post 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.
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
User avatar
STARGÅTE
Addict
Addict
Posts: 2234
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: PB 6.0 and the new map behaviour

Post 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()
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
infratec
Always Here
Always Here
Posts: 7619
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: PB 6.0 and the new map behaviour

Post 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
Last edited by infratec on Thu Sep 15, 2022 9:36 pm, edited 1 time in total.
Little John
Addict
Addict
Posts: 4789
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: PB 6.0 and the new map behaviour

Post 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:
User avatar
jacdelad
Addict
Addict
Posts: 2010
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: PB 6.0 and the new map behaviour

Post 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?!
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
infratec
Always Here
Always Here
Posts: 7619
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: PB 6.0 and the new map behaviour

Post 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?
Little John
Addict
Addict
Posts: 4789
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: PB 6.0 and the new map behaviour

Post 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).
Last edited by Little John on Thu Sep 15, 2022 10:52 pm, edited 1 time in total.
User avatar
STARGÅTE
Addict
Addict
Posts: 2234
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: PB 6.0 and the new map behaviour

Post 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")
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
User avatar
skywalk
Addict
Addict
Posts: 4218
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: PB 6.0 and the new map behaviour

Post 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
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
infratec
Always Here
Always Here
Posts: 7619
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: PB 6.0 and the new map behaviour

Post 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.
Post Reply