Sign in Join
Wiki copytemporary

Namespaces

Since FPC 3.0.0 unit namespaces with dots are supported.

Namespaces in FPC

This page contains proposals and ideas, which could lead to the a better implementation of namespaces that what Delphi currently has. These ideas may or may not work, that's the whole point of this wiki page. Throw ideas around until we find something that could work.

Why?

Namespaces are not perfect, but they do work, and they do greatly reduce the chances for Unit name or Class name conflicts. Other languages and frameworks (Java, .NET, C++ etc) have already seen the problem and introduced namespace support.

How visible is the unit name conflict problem? Just think, how many projects are there that use the unit names (or would like to use these common names):

text
 constants.pas
 utils.pas
 etc...

Extremely common names, and are really good names for what they contain (they describe the purpose of the unit clearly), so why can't we use them in our projects? Namespaces will resolve such problem.

Interested Parties

Usage Examples

Please add any new ideas to the end of the list.

Suggestion 1 - "dotted unit name notation"

Use the same style as implemented in Delphi 2009 onwards.

Pros

Cons

Suggestion 2 - new keyword & new compiler parameter

This idea works similar to Compiler Defines. They can be applied to a unit, or to a project as a whole. Add a new keyword namespace <value> to the Object Pascal language that gets added to the unit line of a source file. Also add a new -namespace=<value> compiler parameter to apply a namespace to a whole project

Example

pascal
   // per unit
  unit utils namespace companyxyz;
  uses
    ...
  end.

...or applied at compile time to all units being compiled.

pascal
   // our sample unit - as normal, nothing new
   unit utils;
   uses
     ...
   end.
text
   $ fpc -namespace=companyxyz utils.pas constants.pas ...

... if a conflict occurs, then you reference the unit with the full namespace.

pascal
     uses
        companyxyz.utils;  // because unit names must be valid identifier and thus,
                           // can't contain dots, when the compiler sees this, it knows
                           // companyxyz. is a namespace reference.

Pros

pascal
 unit buttons namespace fpgui;
 uses
   Classes
  ...
 end.
text
 fpc -namespace=fpgui buttons.pas groupbox.pas panel.pas
pascal
// our project also has a SysUtils unit, but because RTL already has one, we
// must define a namespace for it.
unit SysUtils namespace myproject;
uses
  ...
end.
 
unit mytools;
uses
  SysUtils,             // from the RTL
  myproject.SysUtils,   // from this project / the myproject namespace.
  Classes;

Cons

This would be confusing to any reader (other than the original author). As reading "SysUtils" will always trigger the assumption RTL

Suggestion 3 - directory layout

Similar to what JAVA does, by using the directory hierarchy to define the namespace.

Pros

Cons

Mapping Namespace to each search path

Each installed "package" (set of units) usually resides in a directory, and has it's own /lib or units folder for pre-compiled ppu.

This directory is added to the list of search paths.

Definitions / Proposals

:* Package: A collection of units, grouped in one directory. (Since FPC as no real packages yet)

Examples: RTL, FCL, LCL, SynEdit, CodeTools, RxLib, ....

:* Unit names:

It is not encouraged to call a unit Utils. It should still be attempted to use names with a likelihood for uniqueness (such as RxUtils). The described proposal is a last resort to resolve remaining conflicts

Adding / Creating a Namespace / SearchPath-Alias

Current Situation; Any package used in a project must be known to FPC at compile time of the project. that is archived by adding an -Fu to either the command-line or fpc.cfg file

Allowing to specify Namespace/SearchPath-Alias together with the Path in the -Fu (or alternative a new directive -FU)

Since there is no char, that can not already be part of a directory name in a path, -Fu cannot easily be extended, so that an alias would always be detected.

Using -FU would tell the compiler, that an alias is always expected. The alias always ends at the first occurence of the separator char. e.g -FU LCL : /path/to/lcl (spaces are for readability => real directive, would not have spaces)

This would be set by the supplier of the package, e.g when downloading rxlib, the people who are in charg of rxlib, would have set the default to "rxlib"

This can create conflicts in namespace,. but the user can easily change the default

If the user changes the default, and hiscode refers to the changed default, he must change the default on all installations where he uses this.

Maybe the ability override defaults, in the project settings instead should be discussed? Separate discussin...

Instead of a namespace in -FU or lpk; a namespace can be defined inside the unit(s)

Because there is no package file, this would be needed in every unit. (alternatively a unit with special name (e.g equal to the last directory name in the path ?)

Example

Unit Utils; Namespace 'RxLib'; [1]

Cons: As there currently is no package concept, this would be required for each unit, here would be no way to ensure all units in the path have the same (a consistent) Namespace.

Multiple namespaces in one packages may not be desirable .

-Fu (1, 2) and namespace (3) can work in parallel, -FU takes priority.

The "uses" clause

uses Utils in RxLib;

Note ther e are no quotes around the namespace, this disambiguates it from a directory of the same name, present in the project folder.

uses Utils in RxLib alias RxUtils, utils in LCL alias LCLUtils;

uses Utils in RxLib as RxUtils, utils in LCL as LCLUtils;

alias (or as / to be decided) can be used, if several units of the same name exist across several namespaces, and should all be imported.

No 2 units must resolve to the same name

But that might lead to unnecessary changes in a unit if a unit name is already used to distinguish types like in the following example:

pascal
uses
  Windows, Graphics; // both have a type TBitmap

(...)

var
 b1: TBITMAP; // from Windows
 b2: Graphics.TBitmap; // Note: for this example I assume that the first unit "wins". If FPC handles this different, then please exchange the unit in the uses clause ^^

Assuming there are more usages of "Graphics.TBitmap" in that unit the addition of another Graphics unit would lead to unnecessary changes if both units must be aliased:

pascal
uses
 Windows, Graphics in LCL, Graphics in SomeGraphicsPkg as SomeGraphics;
 // Note: I just picked one of the syntaxes for this example

uses RxLib.Utils;

uses RxLib.Utils as RxUtils;

uses RxUtils=RxLib.Utils;

uses RxUtils(RxLib.Utils);

References in the code

In the code the unit can only be referenced by it's alias (or it's own name, if it was not aliased.

Namespace qualifiers should not be allowed in the code

Namespace qualifiers in the code can cause ambiguity, since a unit and a name of the same namespace can exist. See Mantis report 14439

Namespace qualifiers are not native pascal. Keeping their effect as low as possible (quarantined to the "uses" clause, maybe the "unit" declaration) would be desirable

Pros

Cons

Suggestion 123 - all together

Proposal

Use dotted unit names with additional keywords and mapped to directory layout.

Example

E.g. the current classes unit would become freepascal.rtl.classes.pas

But you can also store it in a directory freepascal/rtl/classes.pas or freepascal.rtl/classes.pas to avoid cluttering a directory with a lot of files and long names.

When using it, you can use a new keyword to not write the full name for every unit.

E.g.

pascal
  uses with freepascal.rtl do begin classes, sysutils end, with lazarus.lcl do begin ...  end, ...;

or allow multiple uses

pascal
  with freepascal.rtl do uses classes, syutils;
  with lazarus.lcl do uses ...
  ...

Pros

Cons

see also


From the Free Pascal wiki, page "Namespaces", as it stood on 2020-08-03 after a change by BrainChemistry, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's Licensing page. If you have concerns about this material, message the admin.

Revision 3, last changed by WikiArchive .

Talk about this page

Nobody has opened a bench for this page yet. Sign in to open one.