Since we were talking about dynamic arrays I was curious to see if they could 
be implemented using the new management operators so I made a little proof of 
concept by cobbling together old code. It’s not complete or very good by any 
means but I think it’s a pretty interesting alternative to using dynamic 
arrays. Being able to add operators and methods is nice, especially since type 
helpers (that we could use on dynamic arrays) are so limited by single scopes 
currently. Specifically it’s nice to just declare an array and say “a += 1” 
without anything else. Having the implementation in a unit is good also in case 
you need to mess with something for specific performant reasons.

Not sure if this is useful or not but it’s certainly a milestone for Pascal to 
be able to make complex data types that integrate so well into the language and 
are so easy to use and manage.

https://github.com/genericptr/Managed-Arrays-Dictionaries

TIntArray = specialize TDynArray<integer>;

var
  a: TIntArray;
begin
  d.AddValues([1, 2, 3]); // := assignment operator for implicit arrays [] are 
broken currently
  a += 4;
  for i in a do
    writeln(i);
end;

TIntDict = specialize TDynDictionary<integer>;

var
  dict: TIntDict;
  entry: TIntDict.TEntryPtr;
begin
  dict.SetValues([
    'a', 1,
    'b', 2
   ]);
  dict.SetValue('c', 3);

  if dict['a'] = 1 then
    writeln('a = 1');

  for entry in dict do
  if entry^.IsValid then
    writeln(entry^.key, ' => ', entry^.value);
end;


Regards,
        Ryan Joseph

_______________________________________________
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Reply via email to