开发者

delphi XE multi-unit namespace question

开发者 https://www.devze.com 2023-03-05 20:07 出处:网络
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 Names

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.开发者_StackOverflow中文版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.


In Delphi.NET (before Prism): unit name = namespace. That's the way they used it in that time - and in dotNET an unit was really an namespace (inclusive appear as such in generated IL).

In native Delphi, I don't see the difference (if that exists at all).

0

精彩评论

暂无评论...
验证码 换一张
取 消