I'm trying to figure out whether it is possible to initialise a record containing a dynamic array using the "implicit" class operator in Delphi (Berlin 10.1 upd 1)
The attached program produces the following output:
ci iA r1 r2 r3
1 1 1 1 49694764491115752
2 2 2 2 11570520
3 3 3 3 0
4 4 4 4 0
5 5 5 5 0
- TRec is the record type containing a dynamic array that I want to initialise.
- ci is a constant array of integer.
- ia is a dynamic array of integer.
- r1,r2,r3 are records of type TRec which are initialised in different ways.
As you can see from the output, the first two assignments (r1,r2), using constants work as expected. The third assignment r3 := iArray
is accepted by the compiler, but the result is broken. The debugger shows that the value of v
in TRec.Implicit
is already wrong.
What is going wrong here? Is this possible at all?
program Project5;
{$APPTYPE CONSOLE}
{$R *.res}
type
TRec = record
iArray: array of UInt64;
class operator Implicit(const v: array of UInt64): TRec;
end;
{ TRec }
class operator TRec.Implicit(const v: array of UInt64): TRec;
var
i: integer;
begin
setlength(Result.iArray, Length(v));
for i := 0 to High(v) do
Result.iArray[i] := v[i];
end;
const
ciArray: array [0 .. 4] of UInt64 = (1, 2, 3, 4, 5);
var
i : integer;
iArray : array of UInt64;
r1, r2, r3: TRec;
begin
iArray := [1, 2, 3, 4, 5];
r1 := [1, 2, 3, 4, 5];
r2 := ciArray;
r3 := iArray;
Writeln('ci iA r1 r1 r3');
for I := 0 to High(ciArray) do
Writeln(ciArray[i], ' ', iArray[i], ' ', r1.iArray[i], ' ', r2.iArray[i], ' ', r3.iArray[i]);
readln;
end.