Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 711
user754458 Avatar asked May 15 '11 12:05

user754458


1 Answers

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.

like image 177
gabr Avatar answered Sep 25 '22 18:09

gabr