gpt4 book ai didi

delphi XE多单元命名空间问题

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

我正在阅读 Delphi XE 中的 RAD Studio 文档。这里有一些文字。

[ Delphi Reference -> Delphi Language Guide -> Programs and Units -> Using Namespaces -> Searching Namespaces -> Multi-unit Namespaces ]

Multi-unit Namespaces

Multiple units can belong to the same namespace, if the unit declarations refer to the same namespace. For example, one can create two files, unit1.pas and unit2.pas, with the following unit declarations:

// in file 'unit1.pas' 
unit MyCompany.ProjectX.ProgramY.Unit1

// in file 'unit2.pas'
unit MyCompany.ProjectX.ProgramY.Unit2

In this example, the namespace MyCompany.ProjectX.ProgramY logically contains all of the interface symbols from unit1.pas and unit2.pas.

Symbol names in a namespace must be unique, across all units in the namespace.
In the example above, it is an error for Unit1 and Unit2 to both define a global interface symbol named mySymbol

我对此进行了测试。代码如下。

----------------------------------------------------------------- 
program Project1;

{$APPTYPE CONSOLE}

uses
SysUtils,
Lib.A in 'Lib.A.pas',
Lib.B in 'Lib.B.pas';

begin
WriteLn ( TestValue ) ;
ReadLn ;
end.
-----------------------------------------------------------------
unit Lib.A;

interface
const TestValue : Integer = 10 ;
implementation

end.
-----------------------------------------------------------------
unit Lib.B;

interface
const TestValue : Integer = 10 ;
implementation

end.

这不是错误。为什么?我不明白。

最佳答案

您的代码与文档不匹配。文档明确指出“unit MyCompany.ProjectX.ProgramY.Unit1”的文件名是unit1.pas,而不是MyCompany.ProjectX.ProgramY.Unit1。

但是,我认为该功能根本没有实现。如果我更改您的代码以将第一个单元存储在文件 a.pas 中,将第二个单元存储在文件 b.pas 中,则单元根本不会编译,错误为

[DCC Error] A.pas(1): E1038 Unit identifier 'Lib.A' does not match file name

(这正是我所期望看到的。)

在您的情况下,没有冲突,因为您始终可以使用“冲突”全局的全名 - Lib.A.TestValue 和 Lib.B.TestValue。

关于delphi XE多单元命名空间问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6008456/

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