Unit-Qualified Identifiers

Similar to namespaces in C++, Java and C#

Conflicts of Identifiers with Identical Names

Multiple units may define types / functions / variables sharing the same name. Units placed later in the uses list will shadow identical identifiers from preceding units.

Unit Qualification Syntax

UnitName.Identifier explicitly specifies the source unit, forcing the compiler to adopt definitions from the designated unit and resolve shadowing conflicts.

The two segments of uses take effect independently

  • interface uses: Only applies to the interface section;
  • implementation uses: Only applies to the implementation section. Identifiers from units referenced in the implementation section shadow identical identifiers from units in the interface section.

Example

Create a unit named Graphics.pas

unit Graphics;
{$mode objfpc}{$H+}

interface

type
  TColor = LongInt;
const
  clYellow = 10;
function Red(C:TColor):Integer;
function Green(C:TColor):Integer;
function Blue(C:TColor):Integer;

implementation
function Red(C:TColor):Integer; begin Result:=C shr 16; end;
function Green(C:TColor):Integer; begin Result:=(C shr 8) and 255; end;
function Blue(C:TColor):Integer; begin Result:=C and 255; end;

end.Code language: JavaScript (javascript)

Then create another unit GoogleMapsEngine.pas

unit GoogleMapsEngine;
{$mode objfpc}{$H+}

interface
type
  TColor = string; // Completely different type from Graphics.TColor

implementation

end.Code language: PHP (php)

We now have two identically named TColor types with different underlying structures

Now write the main program

program showcolor;
{$mode objfpc}{$H+}
{$apptype CONSOLE}

uses Graphics, GoogleMapsEngine;

var
  // Color: TColor; // Error: Resolves to GoogleMapsEngine.TColor (string) as it appears later
  Color: Graphics.TColor; // Correct, explicit unit qualification

begin
  Color := clYellow;
  WriteLn(Red(Color), ' ', Green(Color), ' ', Blue(Color));
  ReadLn;
end.Code language: PHP (php)

Compile and run

If the prefix Graphics. is omitted, TColor defaults to GoogleMapsEngine.TColor. Why?

The main project references units via uses Graphics, GoogleMapsEngine;. Since GoogleMapsEngine is imported second, any duplicate TColor identifier resolves to the later unit by default — GoogleMapsEngine.TColor.

To distinguish them, explicitly write Graphics.TColor, analogous to namespaces. This eliminates naming conflicts entirely.

Faulty Example

Mismatched TColor types between interface and implementation sections trigger compilation errors

Create another unit UnitUsingColors.pas

unit UnitUsingColors;
{$mode objfpc}{$H+}
interface
uses Graphics;
procedure ShowColor(const Color: TColor);

implementation
uses GoogleMapsEngine; // Duplicate TColor from implementation shadows the interface version

// TColor here refers to GoogleMapsEngine.TColor
procedure ShowColor(const Color: TColor); 
begin
end;

end.Code language: JavaScript (javascript)

Reference this unit in the main project mydemo.dpr

program showcolor;
{$mode objfpc}{$H+}
{$apptype CONSOLE}

uses UnitUsingColors;

begin

end.Code language: PHP (php)

A simple reference suffices even without calling its procedures or functions; if unused, the compiler will skip compiling UnitUsingColors.pas entirely.

Breakdown of the compilation error (compiler perspective):

// Interface signature
procedure ShowColor(const Color: Graphics.TColor);
// Implementation signature
procedure ShowColor(const Color: GoogleMapsEngine.TColor);Code language: JavaScript (javascript)

Revise the code as shown below

unit UnitUsingColors;
{$mode objfpc}{$H+}
interface
uses Graphics;
procedure ShowColor(const Color: TColor);
implementation
uses GoogleMapsEngine;
// Explicitly target TColor from Graphics to avoid shadowing by the duplicate type
procedure ShowColor(const Color: Graphics.TColor);
begin
end;
end.Code language: JavaScript (javascript)
unit UnitUsingColors;
{$mode objfpc}{$H+}
interface
uses Graphics;
procedure ShowColor(const Color: TColor);
implementation
uses GoogleMapsEngine;
// Explicit Graphics.TColor unifies the type definition
procedure ShowColor(const Color: Graphics.TColor);
begin
  WriteLn(Red(Color), Green(Color), Blue(Color));
end;
end.Code language: JavaScript (javascript)

Summary

Unit qualification syntax UnitName.Ident lets you precisely specify type origins without adjusting the order of uses clauses, preventing naming conflicts.

Leave a Reply

Your email address will not be published. Required fields are marked *