So here is a question, if unit1 uses classes and unit2 uses unit1 and also uses classes , can unit2 use the classes unit declared in unit1 instead of having classes unit in its own uses section? Logically it should word but if it does it’s bad to not have everything that unit2 uses visible at top of unit.
Every unit
must have
any unit it directly(*) accesses in one of its
uses-sections. That's the whole point of the unit concept, so that it's clear what a unit uses (for both the user and the compiler).
(*) There are some exceptions, e.g. if you use a type only indirectly like here (note: this is a stupid example that has a memory leak, it's only to highlight the indirect use):
Unit
utest.pp:
unit utest;
{$mode objfpc}{$H+}
interface
uses
Classes;
function Test: TStrings;
implementation
function Test: TStrings;
begin
Result := TStringList.Create;
Result.Add('Hello World');
end;
end.
Program
ttest.pp:
program ttest;
{$mode objfpc}{$H+}
uses
utest;
begin
Writeln(Test.Text);
end.
As you can see the compiler knows about the methods of
TStrings inside
ttest despite not having
Classes in the
uses-clause. If you'd want to store the return value of
Test however you'd have to declare a suitable variable and thus declare the
Classes unit in the
uses-clause.