user754458
user754458

Reputation: 193

delphi XE multi-unit namespace question

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.

Upvotes: 11

Views: 2485

Answers (2)

gabr
gabr

Reputation: 26830

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.

Upvotes: 4

Fabricio Araujo
Fabricio Araujo

Reputation: 3820

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).

Upvotes: 0

Related Questions