Am 10.09.2022 um 01:41 schrieb James Richters:
I thought I would make my own IncArray() function:
Procedure IncArray(Var TheArray);
Begin
    SetLength(TheArray,Length(TheArray)+1);
End;

But I get a 'Type Mismatch'  Any ideas how this could be done?  Is this even 
possible without specifying the exact type of array?

You can't simply use a formal parameter for that as SetLength requires information about the type which is missing in that case. What you can do is use a generic procedure:

=== code begin ===

generic procedure IncArray<T>(var aArray: specialize TArray<T>);
begin
  SetLength(aArray, Length(aArray) + 1);
end;

var
  arr: array of LongInt;
begin
  arr := [1, 2, 3];
  specialize IncArray<LongInt>(arr);
end.

=== code end ===

In 3.3.1 you can also enable the modeswitch ImplicitFunctionSpecialization and you can then simply use the following as the compiler is able to derive the type to specialize with:

=== code begin ===

IncArray(arr);

=== code end ===

Regards,
Sven
_______________________________________________
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Reply via email to