在类型声明中在=之后键入什么 [英] What does type after the = do in a type declaration

查看:58
本文介绍了在类型声明中在=之后键入什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在mORMot的SynCommons.pas中有以下代码片段:

In mORMot's SynCommons.pas there is the following snippet of code:

type
  ....
  TTimeLog = type Int64;
             ^^^^

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

What is the purpose of the second type keyword (in front of Int64)?

推荐答案

来自数据类型,变量和常量索引(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和Y属于同一类型;在运行时,
无法将TValue与Real区分开。
通常没有什么意义,但是如果您定义新类型的目的是利用运行时类型
信息,例如,将属性编辑器与特定类型的
属性相关联- 不同的
名称和不同类型之间的区别变得很重要。在这种情况下,请使用
语法:

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

例如:

type TValue = type Real;

强制编译器创建一个新的独特类型,称为TValue。

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

对于var参数,形式和实际类型必须相同。以
为例:

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;


这篇关于在类型声明中在=之后键入什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆