1

I have a library code file which is shared between Delphi 5 and DelphiXE2. I am attempting to add anonymous method functionality to the code file, but only for DelphiXE2 projects (since Delphi 5 doesn't support anonymous methods). It seemed I should be able to use the CompilerVersion (Note: I don't want to limit it to DelphiXE2, just in case we ever upgrade).

{$IF CompilerVersion >= 23}
  {$DEFINE AnonymousAvail}
{$IFEND}

This worked nicely in XE2, but it turns out, Delphi 5 doesn't support the $IF directive. I decided to wrap it in an $IFDEF. This worked nicely in Delphi 5, but XE2 also doesn't seem to have CompilerVersion defined, so AnonymousAvail is not defined.

{$IFDEF CompilerVersion}
  {$IF CompilerVersion >= 23}
    {$DEFINE AnonymousAvail}
  {$IFEND}
{$ENDIF}

Any help would be appreciated.

Note: I cannot move anonymous method code to a different code file.

energ1ser
  • 2,703
  • 4
  • 24
  • 31

1 Answers1

8

Do what the documentation says:

{$IFDEF ConditionalExpressions}
  {$IF CompilerVersion >= 23.0}
    {$DEFINE AnonymousAvailable}
  {$IFEND}
{$ENDIF}

Be sure that the outer condition is as shown (and closed with ENDIF) and you can use CompilerVersion and other constants and expressions inside.

You can also use

{$IF defined(BLAH)}

or, one of my favourites:

{$IF declared(AnsiString)}

etc...


FWIW, I noticed that the example in the link comes, almost verbatim, from my Console.pas unit.

Rudy Velthuis
  • 28,387
  • 5
  • 46
  • 94
  • 1
    @SertacAkyuz yes, I misread that second paragraph. So essentially I was checking if a constant was defined when I needed to check one of the predefined conditionals. – energ1ser Feb 09 '15 at 02:25
  • You can check if a constant is defined, and even query its value. CompilerVersion is a normal Pascal constant, it is not a predefined conditional. – Rudy Velthuis Feb 09 '15 at 07:55