解决方法
a: array of Integer; b: array of Integer;
这两个变量不是分配兼容的。写一个编译错误是:@H_404_9@
a := b;
另一方面,如果您使用通用语法:@H_404_9@
a: TArray<Integer>; b: TArray<Integer>;
那么这两个变量是兼容的。@H_404_9@
当然可以写@H_404_9@
type TIntegerArray = array of Integer;
但所有各方都需要同意同一类型。如果所有的代码都在你的控制之下,这是很好的,但是当使用各种来源的代码时,通用的动态数组的出现会产生巨大的变化。@H_404_9@
弹出的另一个优点就是类似地,你可以使用通用数组类型作为通用方法的返回类型。@H_404_9@
没有通用数组,你就不得不声明一个这样的形式:@H_404_9@
TArrayOfT = array of T
在你的泛型班,这是相当凌乱。而且,如果您在非泛型类中编写通用方法,那么您无法做出该声明。通用数组再次解决了这个问题。@H_404_9@
TMyClass = class class function Foo<T>: TArray<T>; static; end;
这一切都来自于documentation中描述的类型兼容性规则,如下所示:@H_404_9@
Type Compatibility@H_404_9@
Two non-instantiated generics are considered assignment
compatible only if they are identical or are aliases to a
common type.@H_404_9@Two instantiated generics are considered assignment compatible if the base types are identical (or are aliases to a common type) and the type arguments are identical.@H_404_9@