Namespaces in Delphi
Asked Answered
C

3

9

Are there any practical benefits in using long unit file names like MyLib.MyUtils.pas or it is just a kind of unit name prefix?

Chapiter answered 20/1, 2010 at 9:12 Comment(0)
B
14

Namespaces, like all identifiers, are meant to organize.

So using them, only benefits if your project gets organized in a better way. This highly subjective matter (there have been 'wars' on even the most simple naming conventions!), so impossible to really answer.

Here is some documentation on how namespaces work in Delphi.

Note that 'true' namespaces (where more than one generic DLL can contribute to the same namespace; this is how namespaces function in the .NET world) are not possible in Delphi: you could go the BPL way, but that is not the same as a 'generic DLL'. This is not a limitation of Delphi itself, but the way that native DLLs in Windows 'work'.

--jeroen

Brothers answered 20/1, 2010 at 10:40 Comment(4)
+1 interesting link. I didn't even know that there are "official namespaces" in Delphi.Whitted
Looks like the doc article was written for Delphi.NET (dcuil file etc). Embarcadero must edit it to document Delphi for Win32 namespaces.Chapiter
+1 thanks for the link... Now I'm looking for any reference of which versions support these namespace prefixes...Endomorphic
@JerryDodge from what I remember, any Delphi version after Delphi 7 supports it. Delphi shipped with the preview version of the Delphi .NET commandline compiler, from then on every Delphi .NET compiler supported it, and I think any native Delphi compiler supported it too.Brothers
A
3

See:

Why does Delphi (dcc32.exe) have an option to set a “Namespace search path”?

Namespaces in Delphi are supported by the compiler, so the dot in the unit names has a special meaning. You can use the full qualified name of the file, or you can use a 'shortcut' style for the file name if the dcc32.exe option is set up properly.

So you could also write

uses
  MyBestTools;

to avoid the full qualified name

uses
  MyCompany.MyProject.MyLibrary.MyBestTools;

Disclaimer: currently this is all theory but I will make use of namespaces in some projects iin the near future

For an even better IDE support, feel free to vote in QualityCentral for this feature suggestion:

Visualize the unit namespaces hierarchy in the IDE

Acuate answered 20/1, 2010 at 11:17 Comment(4)
I have found the same options in IDE (Delphi 2009): Project/Options/Delphi Compiler/Default Namespace, NameSpace PrefixesChapiter
In Delphi XE7 IDE it is called "Unit scope names"Richela
@Richela does XE7 still have the -NS<namespaces> = Namespace search path parameter for dcc32?Acuate
@Acuate yes, it's just the IDE option name that changedRichela
W
1

I edited this answer in response to the comments and my obvious misunderstanding of the question.

The only practical benefit I see in using unit names like you suggest is that the IDE will sort the units in a better way than without the "namespace" prefix. I would prefer using different folders instead.

In some cases it might make sense - because of name collisions. You could for example have a unit MyLib.Utils and a unit MyOtherLib.Utils This can indeed avoid some confusion (both for the IDE and for yourself).

You could also simulate a namespace using an abstract class with static class methods:

type
  Utils = class abstract
    class procedure Beep; static;
  end;
...
Utils.Beep;   
Whitted answered 20/1, 2010 at 9:17 Comment(6)
If I understand Serg right he asks about dots inside (not after) unit names.Turncoat
You might be right, since the question is not very detailed. I edited my answer to reflect this possibility.Whitted
Sure the question is about "dotted" unit names. As far as I understand the dot is just another symbol in unit name prefix and nothing more. For example, you can't write "uses MyLib" to use MyLib.MyUtils or use "MyLib" identifier in any other manner.Chapiter
okay, I think I got it. I edited the answer. Hopefully it's more helpful now.Whitted
@Serg: DCC32.exe has a special option to set the default namespace. So you can use MyUtils if you set the default name space to MyLib.Acuate
A namespace can not Beep. It organizes classes. Compare beeptools.Buzzer.makeNoise() and Buzzer.makeNoise() for exampleAcuate

© 2022 - 2024 — McMap. All rights reserved.