이 예시처럼 동적 배열을 쓰는 방법도 있습니다:
var
CachedLabels: TArray<string>;
function ReadLabels: TArray<string>;
begin
if Length(CachedLabels) = 0 then
CachedLabels := TArray<string>.Create('Alpha', 'Beta', 'Gamma');
Result := CachedLabels;
end;
힙에 있는 동적 배열을 런타임에 초기화하지만, 사용 위치에서 바로 초기화할 수 있게 하는 Delphi 동적 배열의 "유사 생성자"를 보여 줍니다. (주의: 이 예시는 스레드 안전하지 않습니다).
배열 길이는 표준 Length() 함수로 구하면 되고, 유효한 인덱스 범위는 표준 Low()와 High() 함수로 알아낼 수 있습니다.
이전 Delphi 버전에서는 TArray 대신 직접 정의한 동적 문자열 배열 타입을 쓰면 됩니다. 예를 들어:
type
TLabelArray = array of string;
영어에서 번역·원문 보기You can also use dynamic arrays, as in this example:
var
CachedLabels: TArray<string>;
function ReadLabels: TArray<string>;
begin
if Length(CachedLabels) = 0 then
CachedLabels := TArray<string>.Create('Alpha', 'Beta', 'Gamma');
Result := CachedLabels;
end;
Although it initializes a heap-allocated dynamic array at run-time, this demonstrates Delphi's dynamic-array "pseudo-constructor", which permits initialization right where it is used. (NOTE: this example is not thread-safe).
To obtain the array's length, simply call the standard Length() function, or call the standard Low() and High() functions to determine the valid range of indices.
For older Delphi versions, substitute your own dynamic string-array type for TArray, for example:
type
TLabelArray = array of string;