Delphi array initialization

淺唱寂寞╮ 提交于 2019-11-30 17:14:00
Gerry Coll

Arrays of records can be intialised in const expressions:

const
  Points : TPointArray = ((X: 1; Y: 1), (X:1; Y:2), (X:1; Y:1), (X:1; Y:1));

class function rotationTable.offsets(pType, rotState, dir: integer): TpointArray;
begin
   Result := Points;
end;

In XE7 it is possible to fill a dynamic array of records like this:

function GetPointArray: TArray<TPoint>;
begin
  Result := [Point(1,1),Point(1,2),Point(1,1),Point(1,1)];
end;
Rob Kennedy

Plainth's answer demonstrates the constructor-like syntax for dynamic arrays. You can use that directly on a TPoint array to yield a much simpler helper function:

type
  TPointDynArray = array of TPoint;
  T4PointArray = array[0..3] of TPoint;

function PointDynArrayTo4PointArray(const input: TPointDynArray): T4PointArray;
var
  i: Integer;
begin
  Assert(Length(input) = Length(Result));
  for i := 0 to High(input) do
    Result[i] := input[i];
end;

class function rotationTable.offsets(pType, rotState, dir: integer): T4PointArray;
begin
  // New dynamic-array-constructor syntax here
  Result := PointDynArrayTo4PointArray(TPointDynArray.Create(
    Point(1,1), Point(1,2), Point(1,1), Point(1,1)));
end;

But that's overkill. Delphi also lets you define open arrays inline, and there's no additional constructor call to write. The result uses your original proposed syntax, but with the array wrapped inside a function call. It will work in all Delphi versions, whereas the "Create" syntax above is fairly new.

function PointOpenArrayTo4PointArray(const input: array of TPoint): T4PointArray;
var
  i: Integer;
begin
  Assert(Length(input) = Length(Result));
  for i := 0 to High(input) do
    Result[i] := input[i];
end;

class function rotationTable.offsets(pType, rotState, dir: integer): T4PointArray;
begin
  Result := PointOpenArrayTo4PointArray(
    [Point(1,1), Point(1,2), Point(1,1), Point(1,1)]);
end;

You might want to consider using Gerry's answer just to give your arrays of points meaningful names, which might help when debugging and one of the eight magic numbers in those point definitions is wrong.


Finally, a note on what Delphi meant when it said "the [1, 2, 3, 4] syntax can only work for Integers." That syntax defines a set, not an array. You can't have a set of record values, but you can have a set of integers. A side effect is that the syntax for a set of integers is the same as the syntax for an open array of integers. I think Delphi uses context to figure out which one you mean, but it can sometimes guess wrong.

You cannot because you cannot express in the code body a point in the way in which you can express it in the const section.

However you can do some tricks in order to have your life easier, especially if you have a reasonable number of points.

You can implement a simple procedure like this (code not tested):

procedure BlendDimensions(aXArray, aYArray: TIntegerDynArray; var aResult: TPointArray);
var
  nCount: integer;
  i: integer;

begin
  nCount:=High(aXArray);
  if nCount <> High(aYArray) then 
    Exception.Create('The two dimension arrays must have the same number of elements!');

  SetLength(aResult, nCount);
  for i:=0 to nCount do
  begin
    aResult[i].X:=aXArray[i]; //simple copy
    aResult[i].y:=aYArray[i];
  end;
end;

...where TIntegerDynArray is the RTL's dynamic array of integers. (In fact it will work with any dynamic array). Also, TPointArray in the above example is also dynamic.

So, in order to do your job, you can do like this:

procedure Foo;
var
  myXCoords, myYCoords: TIntegerDynArray; //temp arrays
  myPoints: TPointArray; //this is the real thing

begin
  myXCoords:=TIntegerDynArray.Create( 1, 2, 3, 4, 5, 6, 7, 8, 9,10);
  myYCoords:=TIntegerDynArray.Create(21,32,34,44,55,66,65,77,88,92); //...for example 
  BlendDimensions(myXCoords, myYCoords, myPoints); //build the real thing
 //use it...
end;

Things to note:

  • You see clearly which are your points
  • You can be very productive in this way
  • You can use BlendDimensions also on other things not only on this one
  • You can easily expand BlendDimensions for 3 (or more) dimensions
  • ...but beware because a copy is involved. :-) With today PCs, the weakpoint will be, by far, your hand. :-) You will get tired to type much more quickly till the copy time will be even noticed.

HTH

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!