Variant open array parameters
allow you to pass an array of differently-typed expressions to a single
procedure or function. To define a routine with a variant open array parameter,
specify array of const as the parameter’s type. Thus
procedure
DoSomething(A: array of const);
declares a procedure called DoSomething
that can operate on heterogeneous arrays.
The array of const
construction is equivalent to array of TVarRec. TVarRec,
declared in the System unit, represents a record with a variant part
that can hold values of integer, Boolean, character, real, string, pointer,
class, class reference, interface, and variant types. TVarRec’s VType
field indicates the type of each element in the array. Some types are passed as
pointers rather than values; in particular, long strings are passed as Pointer
and must be typecast to string.
The following example uses a
variant open array parameter in a function that creates a string representation
of each element passed to it and concatenates the results into a single string.
The string-handling routines called in this function are defined in SysUtils.
function
MakeStr(const Args: array of const): string;
const
BoolChars: array[Boolean] of Char = ('F', 'T');
var
I: Integer;
begin
Result := '';
for I := 0 to High(Args) do
with Args[I] do
case VType of
vtInteger: Result := Result +
IntToStr(VInteger);
vtBoolean: Result := Result +
BoolChars[VBoolean];
vtChar: Result :=
Result + VChar;
vtExtended: Result := Result +
FloatToStr(VExtended^);
vtString: Result := Result +
VString^;
vtPChar: Result :=
Result + VPChar;
vtObject: Result := Result +
VObject.ClassName;
vtClass: Result :=
Result + VClass.ClassName;
vtAnsiString: Result :=
Result + string(VAnsiString);
vtCurrency: Result := Result +
CurrToStr(VCurrency^);
vtVariant: Result := Result + string(VVariant^);
vtInt64: Result :=
Result + IntToStr(VInt64^);
end;
end;
We can call this function using an
open array constructor (see Open
array constructors). For example,
MakeStr(['test', 100, ' ', True,
3.14159, TForm])
returns the string “test100 T3.14159TForm".
Procedures and functions: Overview
变体开发数组参数允许向单个过程或函数传递不同类型表达式的数组。要定义含有变体开放数组参数的例程,需要指定array of const作为参数类型。如,
procedure
DoSomething(A: array of const);
这里声明了一个叫做DoSomething的过程,该过程可以操作不同种类的数组。
array of const结构等价于array of TVarRec。TVarRec,在单元System中声明,用于表示一个记录,记录中有一个可以保存多种值(整数、布尔、字符、实数、串、指针、类、类引用、接口、变体等)的变体部分。TVarRec中的VType字段表示数组中每个元素的类型。一些类型作为指针传递而不传递值;特别是长串,作为指针传递并且必需转换为string类型。
下面的例子在函数中使用了变体开放数组参数,该函数对接受的每个元素创建一个串表示法,最后连接成一个串。该函数中调用的串处理例程都定义在SysUtils单元中。
function
MakeStr(const Args: array of const): string;
const
BoolChars: array[Boolean] of Char = ('F', 'T');
var
I: Integer;
begin
Result := '';
for I := 0 to High(Args) do
with Args[I] do
case VType of
vtInteger: Result := Result +
IntToStr(VInteger);
vtBoolean: Result := Result +
BoolChars[VBoolean];
vtChar: Result :=
Result + VChar;
vtExtended: Result := Result +
FloatToStr(VExtended^);
vtString: Result := Result +
VString^;
vtPChar: Result :=
Result + VPChar;
vtObject: Result := Result +
VObject.ClassName;
vtClass: Result :=
Result + VClass.ClassName;
vtAnsiString: Result :=
Result + string(VAnsiString);
vtCurrency: Result := Result +
CurrToStr(VCurrency^);
vtVariant: Result := Result + string(VVariant^);
vtInt64: Result :=
Result + IntToStr(VInt64^);
end;
end;
可以用一个开放数组构造器(见开放数组构造器)来调用该函数。例如:
MakeStr(['test', 100, ' ', True,
3.14159, TForm])
上面的调用将返回串 “test100 T3.14159TForm"。
编者注
TVarRec类型用于含有array of const类型的参数的函数内部。该类型声明于预定义单元System中,声明如下:
const
vtInteger = 0;
vtBoolean = 1;
vtChar = 2;
vtExtended = 3;
vtString = 4;
vtPointer = 5;
vtPChar = 6;
vtObject = 7;
vtClass = 8;
vtWideChar = 9;
vtPWideChar = 10;
vtAnsiString
= 11;
vtCurrency = 12;
vtVariant = 13;
vtInterface = 14;
vtWideString
= 15;
vtInt64 = 16;
type
PVarRec = ^TVarRec;
TVarRec = record
case Byte of
vtInteger: (VInteger: Integer; VType: Byte);
vtBoolean: (VBoolean: Boolean);
vtChar: (VChar: Char);
vtExtended: (VExtended: PExtended);
vtString: (VString: PShortString);
vtPointer: (VPointer: Pointer);
vtPChar: (VPChar: PChar);
vtObject: (VObject: TObject);
vtClass: (VClass: TClass);
vtWideChar: (VWideChar: WideChar);
vtPWideChar: (VPWideChar: PWideChar);
vtAnsiString: (VAnsiString:
Pointer);
vtCurrency:
(VCurrency: PCurrency);
vtVariant: (VVariant: PVariant);
vtInterface: (VInterface: Pointer);
vtWideString: (VWideString:
Pointer);
vtInt64: (VInt64: PInt64);
end;
编译器自动将数组中的每个元素转换为TVarRec值。VType字段表示每个TVarRec值的简单类型,变体类型常量表示为传递到TVarRec结构中的标记字段的值。
原始资料见有关TVarRec type的联机帮助