将Append方法添加到通用TArray类型

问题描述 投票:0回答:1

我正在尝试将Append方法添加到System.Generics.Collections.TArray类型。

unit uMyArray;

interface

uses
  System.Generics.Collections;

type
  TArray = class(System.Generics.Collections.TArray)
  public
    class procedure Append<T>(var AValues: array of T; const AItem: T); static;
  end;

implementation

class procedure TArray.Append<T>(var AValues: array of T; const AItem: T);
begin
  SetLength(AValues, Length(AValues) + 1);
  AValues[Length(AValues) - 1] := AItem;
end;

end.

编译时,在SetLength行上出现以下错误:

[[dcc32错误] uMyArray.pas(18):E2008不兼容的类型

arrays delphi generics delphi-xe7
1个回答
3
投票

您无法调整开放数组参数的大小。您需要传递TArray<T>

更改

class procedure Append<T>(var AValues: array of T; const AItem: T); static;

to

class procedure Append<T>(var AValues: TArray<T>; const AItem: T); static;
© www.soinside.com 2019 - 2024. All rights reserved.