[fpc-pascal] Generic routines for both dynamic array and other collections

Sven Barth pascaldragon at googlemail.com
Mon Feb 22 08:00:07 CET 2021


James Richters <james.richters at productionautomation.net> schrieb am Mo.,
22. Feb. 2021, 01:07:

> I've been using a lot of dynamic arrays of records lately,  Some are even
> Dynamic Arrays of records that contain other records, for example:
> Type
>    XYZ_Record = Record
>       X,Y,Z : Double
>    End;
>
>    Call_Stack_Record = Record
>       Location : LongInt;
>       Offset   : XYZ_Record;
>       Rotation : Double;
>    End;
>
>    Call_Stack : Array of Call_Stack_Record;
>
> Then I do things like:
> Call_Stack[I].Offset.X := 12.345
>
> Making a helper to handle "All Possible Dynamic Arrays" would have to
> handle this kind of dynamic array of record structure...
>

No. Assuming generic helpers would be supported it would be something
like(!) this:

=== code begin ===

type
  generic TArrayHelper<T> = type helper for array of T
  private
    function GetCount: SizeInt; inline;
  public
    property Count: SizeInt read GetCount;
  end;

function TArrayHelper.GetCount: SizeInt;
begin
  Result := Length(Self);
end;

type
  TLongintArrayHelper = specialize TArrayHelper<LongInt>;

var
  arr: array of LongInt;
begin
  SetLength(arr, 5);
  Writeln(arr.Count);
end.

=== code end ===

In fact this is already possible for named array types.

No special handling for your situation needed. And the generated code
itself would be similar as well due to the inline.

Doesn't change my opinion though that we don't *need* this.

Regards,
Sven
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20210222/c0cbe85b/attachment.htm>


More information about the fpc-pascal mailing list