4

I want to use a unit if it exists. Is there something like {$IF Declared(MyUnit)} for units, or a different way?

I installed a demo version of a component package, and would like to conditionally use units from it, and conditionally add menu items for testing its functionality. All without forcing everybody to install the demo package.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
Uli Gerhardt
  • 13,748
  • 1
  • 45
  • 83
  • I'm just curious, why you would such a thing? – RBA May 22 '17 at 12:06
  • I installed a demo version of a component package, and would like to conditionally use units from it, and conditionally add menu items for testing its functionality. All without forcing everybody to install the demo package. – Uli Gerhardt May 22 '17 at 12:08
  • You can't do this. The only way to get the compiler to search is to name the unit in a uses clause. You need to introduce your own conditional. – David Heffernan May 22 '17 at 12:10
  • 1
    I guess you could check for `{$if declared(SomeIdentifierInThatUnit)}`. – Rudy Velthuis May 22 '17 at 12:20
  • 3
    @RudyVelthuis, that would still require to have a `uses ThatUnit` somewhere before that conditional. Otherwise it will always fail. – Uwe Raabe May 22 '17 at 12:24
  • @UweRaabe: Of course. But that seems not to be the problem. – Rudy Velthuis May 22 '17 at 12:32
  • @David: I simply misunderstood the question. Anyway, I would either use a conditional (like you wrote), or have some mock units. – Rudy Velthuis May 22 '17 at 13:18
  • Whatever you do, it will be ugly. To add one option to the ugliness: Place some dummy units with the same name in a separate folder and add it to the end of your search path. If the demo version is installed, make sure the path to its units is placed before the dummy path. That way either the dummy unit or the correct one is found and you can apply @RudyVelthuis suggestion. – Uwe Raabe May 22 '17 at 14:21
  • Using mocks units, especially empty ones, would indeed make the `{$if declared(someidentifier)}` trick possible again. – Rudy Velthuis May 22 '17 at 19:31
  • Thanks to all for the various ideas. I'm trying to minimize the differences to the "normal" code, so I'll go for an explicit preprocessor define. – Uli Gerhardt May 24 '17 at 06:37
  • It's 2022. Has Delphi make this possible? I'm making open source software, if a developer doesn't have CodeSiteLogging unit, to make it so the software will still compile without it. I need something like {IF EXISTS}CodeSiteLogging;{ELSE}; in the uses clause... then in the program to have the check also. – Greg T Nov 09 '22 at 11:17
  • @GregT I don't think there has changed anything since 2017. You can do it like Synopse does it in https://github.com/synopse/mORMot/blob/master/SynTaskDialog.pas with `USETMSPACK`. – Uli Gerhardt Nov 10 '22 at 07:03

1 Answers1

3

This isn't possible. You would need there to be a condition statement that performed search for a unit. Looking in the units listed in the project file, and then the search paths. However, there is no such conditional.

The best you can do is to use these units only if a conditional symbol is defined. You would then need to define that symbol in your project.

This is obviously inconvenient but it's only necessary while you evaluate the component. Once the evaluation is complete you will adopt it universally, or not at all. In the meantime you will have to live with a degree of awkwardness.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490