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

The difference has to do with where you're allowed to refer to the things that AConsts has in its interface section. In the first AUnit, you could use Const4 to declare a fixed-size array in that interface section. You couldn't do that in the second AUnit because Const4 isn't in scope.

It can have an effect on the compiled program, if you're not careful. Suppose we have another unit that also declares a constant named Const4:

unit BConsts;
interface
const
  Const4 = 50;
implementation
end.

Now we define an array in UnitA like this:

unit AUnit
interface
uses BConsts;
var
  data: array[0..Pred(Const4)] of Integer;
implementation
uses AConsts;
procedure Work;
var
  i: Integer;
begin
  for i := 0 to Const4 - 1 do begin
    data[i] := 8;
  end;
end;
end.

That code will write beyond the end of the array because the Const4 that's in scope in the interface section is not the same Const4 that's used in the implementation section. This doesn't happen often with constants. It usually just happens with two identifiers, the FindClose function defined in Windows and SysUtils, and TBitmap, defined in Graphics and Windows. And in those two cases, the compiler will tell you that you've done something wrong, although it won't tell you precisely that you've used an identifier that has two different meanings. You can resolve the problem by qualifying the identifier:

for i := 0 to BConsts.Const4 - 1 do
  data[i] := 8;

If all the above precautions are addressed, so your program compiles and runs correctly, then it makes no difference where units are used. In your example with App1 and App2, the two programs will be the same. They won't be identical — the compiler will have processed things in a different order and thus will likely put things in different places — but it will have no effect on the execution of your program.


I put all references in the implementation section and only put those unit names in the interface that I have to.

I like to limit the scope of everything as much as possible, though, and this policy is pursuant to that.


The IDE also uses how where you declare your uses to ascertain what it needs to compile.

If your interface section uses UnitA and your implementation section uses UnitB then, if unit B needs recompiling, your unit won't, but if unitA changes then your unit will need to be recompiled.

It's one of the secrets of Delphis super fast build speed.

As to your finished executable, I expect that it will turn out the same size wherever you put the declarations (the linker is clever and only links in methods etc that are actually used by your application), but the actual location of various sources would almost certainly change if the order of the unit declarations is changed.

Tags:

Delphi