2

One feature I miss in Delphi (I hope it is at all possible) is that I cannot have Units automatically include their dependent units. This is possible in c++ headers. For example, in c++:

dependentHeader.h:

#include "baseHeader.h"

Any headers included in baseHeader.h are available in dependentHeader.h. Another example is the precompiled header, whatever I include in the precompiled header is available to all header files in the project. This is very useful for including frequently used headers throughout a project.

Now back to Delphi: I have a Unit called DebugService In order to use it other units are required: DependentUnit1, DependentUnit2.

So in every Unit I use DebugService I have to manually add all the other dependent units: DependentUnit1, DependentUnit2.

What I want is just to be able to specify DebugService as a dependency and have all its dependencies come along?

So, in other words I want:

uses
  DebugService;

and NOT:

uses
  DebugService, DependentUnit1, DependentUnit2;

Is this at all possible?

Thank you!

8
  • 3
    I can't imagine why you think C++ include files are so great. It's a total nightmare that one include brings in all the other includes of that unit. Jun 24, 2013 at 22:31
  • 1
    Delphi's unit system has its own weaknesses, although not what you highlight in my view. I think both languages could be a lot better. What bugs me about Delphi is the lack of namespaces. Jun 25, 2013 at 5:47
  • 1
    @WarrenP I don't think it is a mess. DebugService is a Service so in order to access it you need to use ServiceManager.GetService, which is of course another Unit. If you use DebugService then you will very likely want to display some info and you need to specify a Color (integer). Colors are defined in Vcl.Graphics. So if I want to use DebugService I already need to specify 3 Units. Am rather new to Delphi, and I find this quite annoying. Is there any refactoring tool/add-on that can add the 'uses' dependencies automatically?
    – santiagoIT
    Jun 25, 2013 at 20:01
  • 1
    There is a bit of IDE magic that will automatically add ALL used classes to your unit, if your units (a) require the other units in their Interface uses area, not in implementation, and (b) if you drop a component onto your form, you will get all the necessary units automatically added to your code. You can just drop a non visual component onto the form, and get all the units added without writing any code.
    – Warren P
    Jun 25, 2013 at 22:00
  • 1
    Also there is a way to find what unit defines a particular thing you need, in the refactoring menus. Personally I find it beneficial to add units one at a time, intentionally, and to organize these units into sections. Common libraries (VCL/RTL) first, then third party components, then my own application units. A readable uses clause is part of good Delphi code hygiene. A proper namespace system is what Delphi lacks, right now.
    – Warren P
    Jun 25, 2013 at 22:07

4 Answers 4

12

Ironic that you would ask this, when a better question would be, "Why doesn't C++ have modules yet, in the year 2013".

Delphi's compilation units are not normally split into duplicate .h and .cpp files. You may have noticed that Delphi units have an Interface and Implementation section. This in turn becomes a true module system, compiled .DCU files differ significantly from C++/C compiler ".obj" files because just the interface area can be read, very quickly, by a compiler, when a "uses UnitX" is encountered.

Recently, CLANG/LLVM compiler developers at Apple started adding the rudiments of true module support to the latest CLANG/LLVM C and Objective-C compilers. This means that precompiled header support in XCode is no longer the preferred manner of doing things, because true modules are better than precompiled headers. You could say that a precompiled header system is like having one module, and only one module, as a shabby kludge that you are happy to have, when you cannot have the real thing, which is called Modules. You may say, you are a windows developer, what do you care about CLANG/LLVM? Just that it is evidence that the world is slowly giving up on precompilation, and moving eventually, to modules. The C++ standardization comittee, working at its current rate will certainly deliver you a working C++ standard (but not an implementation) by 2113, at the latest.

In short we might say your question might be asking, if the Horseless Carriage is going to gain features allowing it to accelerate the caching and rapid deployment of Oats to the Equine Power Units.

We don't need that here. We have a real compiler with real module support. End of story. You may notice that Modules (in clang/llvm) are faster than precompiled headers. They are also less of a source of problems, than precompiled headers which are a nearly endless source of crazy problems.

4
  • 7
    +1 Perhaps not the most impartial answer, and possibly not 100% on topic, but I really enjoyed reading this!! :-) Jun 24, 2013 at 22:28
  • but Delphi modules are flat, not nested. I understand that was not an issue on 64KB RAM DOS machines, but it was not updated since those days... Jun 26, 2013 at 23:19
  • When I add something to the Uses clause, and then that in turn causes more things to be required to actually build a project, the IDE should helpfully prompt me and add the units that are required. That is done in limited cases, such as for form-global-variables but should be extended.
    – Warren P
    Jun 27, 2013 at 2:22
  • Just today I decided to use C++ headers to aggregate a bunch of header dependencies. I enjoy that in C++, in contradiction to my answer, but another way to say all of what I said above is that Python is not C++ is not Pascal. In each language, one should learn what is idiomatic and do it. Not to write C++ in Python, or C++ in Pascal.
    – Warren P
    Jun 29, 2013 at 22:37
6

Pre-compiled headers don't have any semantic meaning that differs from standard headers. They are simply an optimisation to improve compilation times. Typically Delphi compilation much faster than C++ compilers and so the optimisation is not needed.

You cannot use unit A and transitively use all of unit A's dependencies. If you want to use definitions from a unit, it must be listed in the uses clause.

5

There is no equivalent to pre-compiled headers in Delphi. Adding the additional uses references is required if DebugService uses declarations from DependantUnit1 and DependentUnit2 in its own declarations of its interface section, and its declarations are then used by other units, thus they are dependant on those other units. If you can design your units to reduce interface dependencies, using dependent units only in the implementation section instead, then you won't have to include DependantUnit1 and DependantUnit2 in other units' uses clauses anymore. But I understand that is not always possible.

If you need to share code amongst multiple units, it is best to move that code to its own unit/package.

4
  • That is too bad!!! One of the dependent Units is Vcl.Graphics. This unit contains the definition of clRed, clBue and other colors. I find it very annoying to have to include Vcl.Graphics everytime I want to use one of those color definitions, which basically happens all the time when DebugService is used...
    – santiagoIT
    Jun 24, 2013 at 22:17
  • @santiagoIT it really can't be that much of an annoyance. Consider how annoying it is, by contrast, to refactor out the need for a dependent, only to find that removing it breaks dozens of other lines dependent on code only implicitly referenced via a higher level include. Now someone has to go scouring through dependentHeader.h to find whatever baseHeader.h that it actually still needs.
    – J...
    Jun 24, 2013 at 23:30
  • 1
    @santiagoIT: Do you really use the actual color values in the interface of DebugService, or just in the implementation? Remember, each can have its own uses clause. If you must use colors in the interface, but only use a few select colors, you could just define your own enum in the interface and then map those values to actual colors in the implementation when needed. That would help to hide the Vcl.Graphics dependency. Jun 25, 2013 at 0:19
  • @RemyLebeau Colors are not used in the interface of DebugService. However units that use DebugService include other units such as ServiceManager (access DebugService) and Vcl.Graphics to specify the color which is used for debugging certain geometries. I find it tedious to have to include these three units every time I want to use DebugService. It is clear to me that I can workaround some of these issues (create a static accesor that hides ServiceManager, define some colors in the interface section so that Vcl.Graphics is not needed). I was just looking for a better approach.
    – santiagoIT
    Jun 25, 2013 at 2:46
1
 #include "baseHeader.h"

is equivalent to

 {$I baseHeader.pas}

you can put anything you like into that file. Even the whole Interface section.

an other alternative to your problem is the use of conditional defines.

in main project file

{$DEFINE debugMyApp} 

in each unit you use

use 
  abc 
{$IFDEF debugMyApp}
   , additionalUNit1
   , additionalUNit2 
   , etc
{$ENDIF}
   ;
1
  • 1
    Please don't do this though. Unless you are the only developer who will ever look at the code. This kind of include file abuse is not a good idea.
    – Warren P
    Jul 2, 2013 at 22:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.