Dotted unit names in Delphi

8

For some time now (probably since Delphi 2005; at least the feature is present in Delphi 2007) Delphi has support for dotted unit names (like myLib.myUtils.pas). A fully qualified unit name (myLib.myUtils.pas) consists of a scope prefix (myLib) and a scoped or partially qualified unit name (myUtils.pas). The compiler supports scope prefixes as can be seen in Project/Options dialog (‘Namespace prefixes’, Delphi 2009):

If you add a scope prefix (myLib) to the list of Namespace prefixes of a project you can use a scoped unit name (myUtils) instead of a fully qualified unit name (myLib.myUtils).

Not much really. I would certanly prefer

uses
  myLib.myUtils;

to messing about with project options just to write a scoped unit name

uses
  myUtils;

Things are changing with Delphi XE2. I have not found any additional support for scope prefixes (only ‘Namespace prefixes’ is renamed by ‘Unit Scope Names’ in Project options dialog), but scope prefixes are extensively used. There are reasons for it.

Most generic unit names are now scoped unit names. For example, ‘Classes.pas‘ now belongs to ‘System‘ namespace and its fully qualified name is ‘System.Classes.pas‘. For the new units using fully qualified names like this

uses
  System.Classes;

is preferable, but to compile a project with a lot of legacy units (or to support previous Delphi versions) a better option is to add ‘System’ prefix to ‘Unit Scope Names’ in Project options dialog.

We now have two GUI application frameworks – VCL and FMX. Many unit names exist in both frameworks, for example VCL.Dialogs.pas and FMX.Dialogs.pas. It is possible to write a framework dependent unit that can be used with both frameworks.

As an example I have written a simple unit that uses Dialogs.pas:

unit TestUnit;

interface

uses
  Dialogs;

procedure Test;

implementation

procedure Test;
begin
  ShowMessage('Hello World!');
end;

end.

I created a VCL Forms application and added a button to the main form:

uses TestUnit;

{$R *.dfm}

procedure TForm17.Button1Click(Sender: TObject);
begin
  Test;
end;

The application was compiled without a hitch because ‘VCL’ scope prefix is already added to project options in VCL application template.

Now I created a FMX Forms application with the same button, tried to build it and got a compile error:

[DCC Fatal Error] TestUnit.pas(6): F1026 File not found: 'Dialogs.dcu' (unit may not be available for the targeted platform)

It appeared that FMX application template does not add ‘FMX’ scope prefix to the list of unit scope names. I don’t know is it done on purpose or it will change in final release.

So I added ‘FMX’ scope prefix manually and got the application compiled, now with FMX implementation of ShowMessage (yes, it looks different from VCL’s one).

Advertisements