Here's an example of the main problem that keeps us from using custom types to
replace dynamic arrays. We basically a need [] property modifier that treats
records the same way the dynamic array syntax does. GetValue could be a
procedure and use a var parameter and maybe even that would be enough. I don't
know what the solutions could be but if the compiler team actually agreed to
approve anything I would be happy to personally implement this myself.
============================
{$mode objfpc}
{$modeswitch autoderef}
{$modeswitch advancedrecords}
program dyanmic_array;
type
TMyRec = record
x, y: byte;
end;
type
generic TArray<T> = record
private type
PT = ^T;
private
data: array[0..1] of T;
function GetValue(index: integer): PT; inline;
procedure SetValue(index: integer; const value: PT); inline;
public
property Values[index: integer]: PT read GetValue write SetValue; default;
end;
TMyRecArray = specialize TArray<TMyRec>;
function TArray.GetValue(index: integer): PT;
begin
result := @data[index];
end;
procedure TArray.SetValue(index: integer; const value: PT);
begin
data[index] := value^;
end;
var
r: TMyRec;
a: TMyRecArray;
begin
r.x := 1;
r.y := 2;
// WRONG: we need to use pointers to write to the array
a[0] := @r;
// CORRECT: we return pointers to the record so we can write to the
underlying data (autoderef must be enabled)
a[0].x := 100;
// CORRET: same as above but with member access and autoderef
writeln('x: ', a[0].x);
// WRONG: to copy the record out of the array we need to derefence the pointer
r := a[0]^;
writeln('r.x: ', r.x);
end.
Regards,
Ryan Joseph
_______________________________________________
fpc-devel maillist - [email protected]
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel