Recent

Author Topic: Native hash-maps in FPC  (Read 623 times)

Alextp

  • Hero Member
  • *****
  • Posts: 722
    • UVviewsoft
Native hash-maps in FPC
« on: September 04, 2018, 10:41:35 pm »
IMO it will be good Pascal extension if FPC supports hash-maps (in python - dictionary).
Must extend Pascal syntax for it.
const V: TMap = [1: 'test', 'zzz': 20, True: 12.34] will be ok.

ps.
DLang 2.x released with hash-maps. (makes native code.)
« Last Edit: September 04, 2018, 10:43:57 pm by Alextp »

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 6627
Re: Native hash-maps in FPC
« Reply #1 on: September 04, 2018, 10:45:38 pm »
D also has garbage collection. Apparently not all its choices are sane :-)

Alextp

  • Hero Member
  • *****
  • Posts: 722
    • UVviewsoft
Re: Native hash-maps in FPC
« Reply #2 on: September 04, 2018, 10:52:21 pm »
Agree, not all choices.

If it's hard for FPC to make such any-type keys (int,str,object...) then at last homo-genous keys (only int, only str....) will be good. It can be TIntHashmap (only int keys), TStrHashmap (only str).

Advantage: easy usage V[2000] or V['key']

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 6627
Re: Native hash-maps in FPC
« Reply #3 on: September 04, 2018, 10:57:18 pm »
In your example there is no typing of the pairs whatsoever. To me this looks like a scripting language construct, not something for a statically compiled language.


Alextp

  • Hero Member
  • *****
  • Posts: 722
    • UVviewsoft
Re: Native hash-maps in FPC
« Reply #4 on: September 04, 2018, 10:59:50 pm »
Agree, need types.
Maybe hashmap can be Generic like THashmap<integer, string>
(1st type for key, 2nd for values)

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 6627
Re: Native hash-maps in FPC
« Reply #5 on: September 04, 2018, 11:01:49 pm »
Well, Maps are library types in FPC. The compiler don't know what a map is.

So you would have to solve that in some way. FPC doesn't have much infrastructure for it.

If you want to implement it, the only example I can think of is maybe the duck typing of for ..in ?

Anyway this all gets horribly complicated for something that is not more than a shorthand. If you have to implement multiple whole new concepts to a language, maybe this feature is not meant to be :-)

Thaddy

  • Hero Member
  • *****
  • Posts: 7195
Re: Native hash-maps in FPC
« Reply #6 on: September 04, 2018, 11:12:01 pm »
Indeed. FPC has some pretty good dictionaries(=maps, a hashmap is a specialzation we also have). in FGL and rtl-generics.
What really is incomprehensible is that you compare it to Python.
Note a hashmap is not a dictionary, it is a specialized form of a dictionary.
It it readily available.
« Last Edit: September 04, 2018, 11:13:58 pm by Thaddy »
inline variables like in D10.3 are a bit like Brexit: if you are given the wrong information it sounds like a good idea. Every kid loves candy, but it makes you fat and your teeth will disappear.

PascalDragon

  • Full Member
  • ***
  • Posts: 206
  • Compiler Developer
Re: Native hash-maps in FPC
« Reply #7 on: September 04, 2018, 11:12:26 pm »
FPC already contains map types (fgl.TFPGMap<,> and Generics.Collections.TDictionary<,> to name two). I see no real reason to add yet another type plus a syntax to initialize them in constant/variable declarations. In the end they are rather complicated data structures so that they can't be easily stored in the binary's data section anyway so you can just as well initialize them in code instead.