Assignment-compatibility is not a
symmetric relation. An expression of type T2 can be assigned to a variable of
type T1 if the value of the expression falls in the range of T1 and at least
one of the following conditions is satisfied.
·T1 and T2 are of the same type, and it is not a file type or structured type that contains a file type at any level.
·T1 and T2 are compatible ordinal types.
·T1 and T2 are both real types.
·T1 is a real type and T2 is an integer type.
·T1 is PChar or any string type and the expression is a string constant.
·T1 and T2 are both string types.
·T1 is a string type and T2 is a Char or packed-string type.
·T1 is a long string and T2 is PChar.
·T1 and T2 are compatible packed-string types.
·T1 and T2 are compatible set types.
·T1 and T2 are compatible pointer types.
·T1 and T2 are both class, class-reference, or interface types and T2 is a derived from T1.
·T1 is an interface type and T2 is a class type that implements T1.
·T1 is PChar or PWideChar and T2 is a zero-based character array of the form array[0..n] of Char.
·T1 and T2 are compatible procedural types. (A function or procedure identifier is treated, in certain assignment statements, as an expression of a procedural type. See Procedural types in statements and expressions.)
·T1 is Variant and T2 is an integer, real, string, character, Boolean, or interface type.
·T1 is an integer, real, string, character, or Boolean type and T2 is Variant.
·T1 is the IUnknown or IDispatch interface type and T2 is Variant. (The variant’s type code must be varEmpty, varUnknown, or varDispatch if T1 is IUnknown, and varEmpty or varDispatch if T1 is IDispatch.)
Type compatibility and identity: Overview
赋值兼容不是对称关系(即类型为A的表达式可以赋值到类型为B的变量,并不意味着类型为B的表达式可以赋值到类型为A的变量)。如果类型为T2的表达式的值落在类型T1的范围之内,并且至少满足下列条件之一,那么类型为T2的表达式可以被赋值到类型为T1的变量:
·T1和T2是相同的类型,并且不是文件类型或结构类型(包括在任何层次包含文件类型的结构类型)。
·T1和T2是兼容的序数类型。
·T1和T2都是实数。
·T1是实数类型,并且T2是整数类型。
·T1是PChar或任何串类型,并且表达式是一个串常量。
·T1和T2都是串类型。
·T1是一个串类型,并且T2是一个标准字符Char或压缩串类型。
·T1是一个长串,并且T2是PChar类型。
·T1和T2是兼容的压缩串类型。
·T1和T2是兼容的集合类型。
·T1和T2是兼容的指针类型。
·T1和T2都是类、类引用或接口类型,并且T2起源于T1。
·T1是一个接口类型,并且T2是实现T1的类类型。
·T1是PChar或PwideChar类型,并且T2是一个形如array[0..n] of Char的零基字符数组。
·T1和T2是兼容的程序型类型。(在某些赋值语句中,程序型类型的表达式被视为函数或过程标识符。见赋值语句和表达式中的程序型类型。)
·T1是变体类型Variant,并且T2是一个整数、实数、串、字符、布尔或接口类型。
·T1是一个整数、实数、串、字符或布尔类型,并且T2是变体类型Variant。
·T1是Iunknown或Idispatch接口类型,并且T2是变体Varian。(如果T1是Iunknown,那么变体的类型代码必需是varEmpty、varUnknown或varDispatch;如果T1是IDispatch ,那么变体的类型代码必需是varEmpty或varDispatch。)