在类型声明中,等号后面的type是什么意思?

8
在mORMot的SynCommons.pas文件中,有以下代码片段:
type
  ....
  TTimeLog = type Int64;
             ^^^^

第二个type关键字(在Int64前面)的目的是什么?

2
这在此主题中有所描述。 - TLama
2
文档说什么? - David Heffernan
1个回答

10

来自数据类型、变量和常量索引(Delphi)

When you declare a type that is identical to an existing type, the compiler treats the new type identifier as an alias for the old one. Thus, given the declarations:

type TValue = Real;
var
  X: Real;
  Y: TValue;

X and Y are of the same type; at runtime, there is no way to distinguish TValue from Real. This is usually of little consequence, but if your purpose in defining a new type is to utilize runtime type information, for example, to associate a property editor with properties of a particular type - the distinction between 'different name' and 'different type' becomes important. In this case, use the syntax:

type newTypeName = type KnownType

For example:

type TValue = type Real;

forces the compiler to create a new, distinct type called TValue.

For var parameters, types of formal and actual must be identical. For example:

type
  TMyType = type Integer;
procedure p(var t:TMyType);
  begin
  end;

procedure x;
var
  m: TMyType;
  i: Integer;
begin
  p(m); // Works
  p(i); // Error! Types of formal and actual must be identical.
end;

啊哈,它使RTTI能够区分类型,这就解释了。 - Johan
5
除了运行时类型识别(RTTI)外,还有许多其他方面需要考虑,它们也会影响到类型身份识别。这是一种编译时的属性。 - David Heffernan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接