[fpc-pascal] Access Violation When SetLength(DynArray, Value)
Sven Barth
pascaldragon at googlemail.com
Thu Sep 15 07:15:02 CEST 2022
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
More information about the fpc-pascal
mailing list