[fpc-pascal] Dynamic arrays using management operators

Ryan Joseph ryan at thealchemistguild.com
Mon Jun 4 11:26:20 CEST 2018


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




More information about the fpc-pascal mailing list