Dotted unit names in Delphi

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

8 thoughts on “Dotted unit names in Delphi

  1. Dotted unit names have been with us since Delphi _7_. Sadly the IDE has failed to catch up with the language. It is better now than it used to be but (as of Delphi 2010 at least) you can still run into problems with dotted unit names when performing simple operations.

    However, that hasn’t stopped me from fully embracing the dot over the past 10 or so years. Now that the VCL and the RTL framework is itself making more and more use of the capability we might see some further improvements in the handling of such things.

  2. I really would like to see this working:

    uses MyLib.Class; <- does not work because "class" is a registered word

    uses MyLib.Gui.*; <- Java like

  3. Dotted unit names are really helpful when organizing your source.

    And I really would like to see something like what Dennis mentioned. If you really seperate classes into their own units you often end up with huge uses lists.

    • Yes, but why would you want to do that? One class per file is ugly enough in Java, but in Delphi it’s even worse, especially with all the circular dependencies it’s gonna cause.

  4. Nice info, thanks. Is it just that pseudo-namespaces are used by the standard libraries in a systematic fashion now, or has compiler support been beefed up in any way too? If the latter, I would assume/hope .NET-style namespaces *haven’t* been implemented on pain of losing simple file-based unit lookup.

    • The feature was available for years, I just tried Delphi 2009 and it works as documented. Probably it was never used because nobody needed it. As I said it was renamed in Pulsar from ‘Namespace prefixes’ to ‘Unit scope names’ .

  5. “The feature was available for years, I just tried Delphi 2009 and it works as documented.”

    Dotted unit names were added has a ‘hidden’ feature in D7, and made a formal feature of the Win32 compiler in D2005 I think.

  6. I’m embarassed to say this, but I avoided dotted-unit-names for years, purely out of loathing for Delphi.net, which is where I first saw a lot of dotted unit names crop up. 🙂

    Now I get that dotted unit names are a nice way of organizing related groups of units, not quite into a package or true namespace, but at least obviously into a visually related clump, I totally love them.

    Warren

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s