delphi XE多单元命名空间问题 [英] delphi XE multi-unit namespace question
问题描述
这里有一些文本。
[Delphi参考 - > Delphi语言指南 - >程序和单位 - >使用命名空间 - >搜索命名空间 - >多单位命名空间]
多单位命名空间
如果单位声明引用相同的命名空间,则多个单位可以属于同一个命名空间。
例如,可以使用以下单位声明来创建两个文件unit1.pas和unit2.pas:
//在文件'unit1.pas'
单元MyCompany.ProjectX.ProgramY.Unit1
//在文件'unit2.pas'
unit MyCompany.ProjectX.ProgramY.Unit2
在这个例子中,命名空间MyCompany.ProjectX.ProgramY在逻辑上包含来自unit1.pas和unit2.pas的所有接口符号。
命名空间中的符号名称必须是唯一的,位于命名空间中的所有单位。
在上面的示例中,它是Unit1和Unit2来定义一个名为mySymbol的全局接口符号
我测试了这个。
代码如下。
------------------- ----------------------------------------------
项目Project1;
{$ APPTYPE CONSOLE}
使用
SysUtils,
Lib.A在Lib.A.pas中,
Lib。 B在Lib.B.pas;
begin
WriteLn(TestValue);
ReadLn;
结束。
---------------------------------------------- -------------------
单位Lib.A;
接口
const TestValue:Integer = 10;
执行
结束。
---------------------------------------------- -------------------
单位Lib.B;
接口
const TestValue:Integer = 10;
执行
结束。
这不是错误。为什么?我不明白。
您的代码与文档不符。文档明确指出单元MyCompany.ProjectX.ProgramY.Unit1的文件名是unit1.pas,而不是MyCompany.ProjectX.ProgramY.Unit1。
我不相信但是,这个功能完全实现了。如果我更改代码以将文件中的第一个单元存储在文件b.pas中的文件a.pas和第二个单元中,则单元根本不进行编译,错误是
[DCC错误] A.pas(1):E1038单位标识符Lib.A与文件名不匹配
(正如我所期待的那样)。
在你的情况下,没有冲突,因为你总是可以使用全局的冲突全局 - Lib.A.TestValue和Lib.B.TestValue。
I am reading RAD Studio Documentaion in Delphi XE. here a some texts.
[ 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
I tested this. code below .
-----------------------------------------------------------------
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.
This is not error. Why? I don't understand.
Your code does not match the documentation. Documentation explicitly states that file name of 'unit MyCompany.ProjectX.ProgramY.Unit1' is unit1.pas, not MyCompany.ProjectX.ProgramY.Unit1.
I do not believe, however, that this feature is implemented at all. If I change your code to store first unit in file a.pas and second unit in file b.pas, units don't compile at all and the error is
[DCC Error] A.pas(1): E1038 Unit identifier 'Lib.A' does not match file name
(Which is exactly as I was expecting to see.)
In your case there's no conflict because you can always use a full name of 'conflicting' global - Lib.A.TestValue and Lib.B.TestValue.
这篇关于delphi XE多单元命名空间问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!