如果有通用属性 getter / setter 在每次访问时执行常见任务,那就太好了。
该代码在 Delphi XE2 中给出编译时错误“E2008 不兼容类型”。类似的代码在编译期间出现内部错误,但从未编译。是我犯了错误还是编译器限制?
type TFoo = class
private
function Get<T>: T;
public
property Bar: Integer read Get<Integer>;
end;
function TFoo.Get<T>: T;
begin
Result := 0;
end;
最佳答案
以下内容在 Delphi 语言中可以是通用的:
- 类(class),例如
TFooClass<T> = class
- 记录,例如
TFooRecord<T> = record
- 接口(interface),例如
TFooInterface<T> = interface
- 程序类型,例如
TFooProc<T> = procedure
- 方法,例如
procedure FooMethod<T>()
属性本身不能是通用的,也不能使用通用的 getter 或 setter 方法来实现。
关于使用泛型的 Delphi 属性 getter 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59408370/