gpt4 book ai didi

delphi - 在类型声明中 = 之后的 type 有何作用

转载 作者:行者123 更新时间:2023-12-03 14:47:34 25 4
gpt4 key购买 nike

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

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

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

最佳答案

来自Data Types, Variables, and Constants Index (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;

关于delphi - 在类型声明中 = 之后的 type 有何作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24185525/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com