显然,以下代码将不起作用:
....
property value: T read FTheValue;
....
function TDefiniteValue<T>.toString: string;
begin
Result:= ' definitly ';
if (value is TObject) then Result:= Result + TObject(value).ToString
else if (value is integer) then Result:= Result + IntToStr(integer(value));
// ^^^^^^^
// +++++++-- integer is not an object
end;
如何比较非对象的类型?
这是一个SSCCE
Program Maybe;
interface
uses
System.Generics.Collections, System.SysUtils;
type
TDefiniteValue<T> = class(TEnumerable<T>)
strict private
FTheValue: T;
strict protected
function toString: string; override;
property value: T read FTheValue;
end;
implementation
function TDefiniteValue<T>.toString: string;
begin
Result:= ' definitly ';
if (value is TObject) then Result:= Result + TObject(value).ToString
else if (value is integer) then Result:= Result + IntToStr(integer(value));
// ^^^^^^^
// +++++++-- integer is not an object.
end;
begin
end.