[fpc-pascal] Generic routines for both dynamic array and other collections
Виктор Матузенко
vitek03 at gmail.com
Sat Feb 20 13:19:26 CET 2021
Hi,
I am trying to write some generic routines for working with containers. For
example, GetLength function:
unit u;
{$MODE FPC}
{$MODESWITCH DEFAULTPARAMETERS}
{$MODESWITCH OUT}
{$MODESWITCH RESULT}
interface
generic function GetLength<TContainter>(const V: TContainter): SizeUInt;
implementation
generic function GetLength<TContainter>(const V: TContainter): SizeUInt;
begin
Exit(V.Count);
end;
end.
// the program
{$MODE FPC}
{$MODESWITCH DEFAULTPARAMETERS}
{$MODESWITCH OUT}
{$MODESWITCH RESULT}
{$MODESWITCH TYPEHELPERS}
uses
u;
type
TDynArray = array of PtrInt;
TDynArrayHelper = type helper for TDynArray
function Count: SizeUInt;
end;
function TDynArrayHelper.Count: SizeUInt;
begin
Exit(System.Length(Self));
end;
var
V: TDynArray;
begin
v := [0, 1, 2, 3, 5, 6, 7, 10, 11, 12, 13, 15, 16];
Writeln(specialize GetLength<TDynArray>(v));
end.
It gives the following compilation error:
u.pas(16,10) Error: Illegal qualifier
Is the problem because the helper is not known while compiling the unit?
How do I write a generic routine GetLength which will work both for dynamic
arrays and for records, objects, classes with Count property?
--
Victor Matuzenko (Виктор Матузенко)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20210220/498987dc/attachment.htm>
More information about the fpc-pascal
mailing list