Difference between adding a unit to the interface or the implementation section

Object Pascal units have two sections (interface and implementation), and as you can see in many source code, each section can have distinct items (uses, types, variables, and constants).

So, which section is suitable for putting a unit as uses ?

First, the main difference is that :

  • Items in the interface are visible in the entire unit, while
  • Items in the implementation are only visible in the implemantation section.

See the three units as example below :

unit A;
interface
const   
cA = 1;
..
unit B;
interface
const
cB = 1;
..
unit C;
interface
uses
A;
const
cC1 = cA;
cC2 = cB; // Error : constant not declared

implementation
uses
B;
const
cC3 = cA;
cC4 = cB;
end.

Also, you can create mutual dependent units if at least one unit is included in the implementation section :

unit A;
interface
implementation
uses  B;
end.
unit B;
interface
implementation
uses  A;
end.

But, if both units are used in the interface section (each unit is used in the interface section of the other unit), the compiling/linking will fail because of circular dependency.

Leave a Reply