Forum > Suggestions
Proposal: Associative String Arrays
Warfley:
So this is probably the thing that most bugs me when doing a Pascal project, that I don't have any easy to use string dictionaries at hand. I'd argue that the probably most useful datastructure aside from the base types and arrays are probably a simple dictionary for String based Key-Value pairs.
There are some types available, e.g. TDictionary and THashMap from Generics.Collections or TFPGMap from fgl, but they don't really work (at least out of the box) with string keys, as they use the pointer to that string as key rather than the string itself.
There are alternatives, like TStringList, and there are some oldschool Hashmap types that map to raw pointers, but they are very cumbersome and inconvinient to use. And that is a staple and a very essential datatype in pretty much all other languages.
So my idea was, that this could be added on the language level as simpe associative array:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---var map: Array[String] of Integer;begin map['foo'] := 42; if 'foo' in map then WriteLn(map['foo']);end;
On a syntactic level this would not be that dissimilar from the existing associative arrays for enums:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---type TTest = (test1, test2);var map: Array[TTest] of Integer;
Of course the underlying technology must be differently, as the above will just create an Array with test1..test2, but from a high level view it would not be that different, and be very useful.
There is already a lot of compiler magic and special treatment for strings, e.g. they work in case-of, and overall are a pretty magical datatype, so adding some more magic would not be that disruptive, and in return I can't stress enough how useful this would be (especially if it's lazy copy and reference counted like normal dynamic arrays).
marcov:
What is wrong with
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} --- {$mode delphi}uses generics.collections; type TStrInt = TDictionary<string,integer>; var map : TStrInt; begin map:=tstrint.create; map.addorsetvalue('foo',42); if map.containskey('foo') then WriteLn(map['foo']); end.
And no, afaik generic.collections has special cuckoo2 hash for string content
Warfley:
Interesting, because in a program where I've used Generics.Collections and I had a bunch of duplicates in the map. Then I must investigate a bit on why that was. Because this has triggered a search for me for other dictionary components that are string capable, and I ended up with some ancient Hashmap implementation. Which is why I created that thread, because using this old Pointer based Datatype made life much harder than it needs to be.
I still think that associative arrays on language level can be useful (one of the things I really like about python), but if generics.Collections works, then there is at least something usable
marcov:
--- Quote from: Warfley on May 11, 2023, 05:09:47 pm ---Interesting, because in a program where I've used Generics.Collections and I had a bunch of duplicates in the map.
--- End quote ---
Note the addorsetvalue vs simply .add.
--- Quote ---I still think that associative arrays on language level can be useful (one of the things I really like about python), but if generics.Collections works, then there is at least something usable
--- End quote ---
It is a typical scripting feature, where everything is a list or map. It is the proverbial hammer in the "if the only tool you have is a hammer" phrase, but fine for quick and dirty batch operations.
I'm not convinced of the need for it a language feature in a non scripting language. Basically it is only curb-appeal for newbs.
Warfley:
It's not just scripting languages, whenever you want to associate data with an identifier that is useful, e.g. when parsing files and wanting to cache that parsing result, or when associating data records with names of the subjects of that record, or when you want to associate connections with usernames in a server (e.g. for a chat).
Just as an example, I wrote a parser which has automatas to parse structures, and for debugging purposes I want to be able to lookup which automata is associated with what symbol
I have not had a single project in the past 5 years where I did not need a dictionary mapping a base type (mostly string) anti a more complex object. It's the natural counterpart to having some form of ID or name in your data field. Whenever you have a data type that has a name or ID, you probably also want some way to look it up.
Navigation
[0] Message Index
[#] Next page