Conditional Uses Clause Considered Harmful

Everyone writing Delphi code that is meant to compile under different Delphi versions has come to the point where conditional compilation seems unavoidable. Ever so often it tends to creep into the uses clauses as well. So, what can we do when units like System.Actions and System.ImageList simple aren’t available in older Delphi versions?

Solution 1: Add an empty unit with the proper name to the project. You might end up with a collection of empty units in your version control system. But hey, it does the job.

Solution 2: Add a unit alias to the project, mapping the unknown units to existing ones you are using anyway. For the two units mentioned above some good candidates are:

  • System.Actions=Vcl.ActnList (for versions below XE3)
  • System.ImageList=Vcl.ImgList (for versions below XE8)

There are a couple of tools that fiddle around with your uses clause from time to time. They will thank you for getting rid of conditions there.

Author: Uwe Raabe

Addicted to Pascal/Delphi since the late 70's

5 thoughts on “Conditional Uses Clause Considered Harmful”

  1. Both solutions require additional effort in comparison to simple conditions inside the uses clause.

    We for example have only one project file for different versions of Delphi (except for packages). In most cases at least. If we would use either solution we would have to use different project files for each version of Delphi (as unfortunately the project options have no possibility to define anything depending of the version of Delphi).

    So we would end up with a big bunch of project files for each project, which we would have to keep in sync. At least for us this is no alternative.

    1. I know that these solutions won’t fit all cases. Actually the idea was born from the IDE plugins I recently made, which already have separate project files for each Delphi version. The reason I came up with this (to avoid conditional defines in uses clauses) in the first place is a tool (and probably future plugin) that does some things with the uses clause and fails when conditional defines exist. The same is true for many other tools doing such things (f.i. ReFind).

      On the other hand, when I read your comment: Would you be interested in a plugin that does at least one of the following?
      a) handles different project options for each Delphi version
      b) keep project files in synch

  2. or… edit the form .pas file in an external editor afterwards – which is the approach I’m using anyway. I build my OTA packages/DLLs using a TakeCommand script and DCC32.
    regards
    Dave.

  3. Solution 3: Fix the tools that fiddle around with your uses clause so that they correctly handle compiler directives.

Comments are closed.