Why {$IFDEF MSWINDOWS} is replaced with {$IF defined(MSWINDOWS)} in Delphi XE5?

Gad D Lord picture Gad D Lord · May 11, 2014 · Viewed 10.8k times · Source

In XE5 all conditional compilations such as

{$IFDEF MSWINDOWS} 

are replaced with

{$IF defined(MSWINDOWS)}

For example System.Diagnostics.pas in XE4 had

...
implementation

{$IFDEF MSWINDOWS}
uses Winapi.Windows;
{$ENDIF}
{$IFDEF MACOS}
uses Macapi.Mach;
{$ENDIF}

{ TStopwatch }
...

and now in XE5 it looks like:

...
implementation
{$IF defined(MSWINDOWS)}
uses Winapi.Windows;
{$ELSEIF defined(MACOS)}
uses Macapi.Mach;
{$ELSEIF defined(POSIX)}
uses Posix.Time;
{$ENDIF}

{ TStopwatch }
...

Is there any particular reason I should migrate my similar invocations as well?

Answer

Johan picture Johan · May 12, 2014

According to the Delphi documentation:

http://docwiki.embarcadero.com/RADStudio/Rio/en/Conditional_compilation_%28Delphi%29

The conditional directives {$IFDEF}, {$IFNDEF}, {$IF}, {$ELSEIF}, {$ELSE}, {$ENDIF}, and {$IFEND} allow you to compile or suppress code based on the status of a conditional symbol.

The {$IFDEF} and {$IFNDEF} only allow you to work with defines previously set by {$DEFINE ...}.
However the {$IF ..} directive is much more flexible, because:

Delphi identifiers cannot be referenced in any conditional directives other than {$IF} and {$ELSEIF}.

const LibVersion = 6;  //One constant to define the libversion.
{$IF LibVersion >= 10.0}
  do stuff that covers LibVersion 10,11 and 12
{$ELSEIF Libversion > 5.0}
  do other stuff that covers LibVersion 6,7,8,9
{$IFEND}

If you tried to do that with defines you'd have to do

{$DEFINE Lib1}
{$DEFINE Lib2}
{$DEFINE Lib3}
{$DEFINE Lib4}
{$DEFINE Lib5}
{$DEFINE Lib6} //all previous versions have to be defined.

{$IFDEF Lib10}
  do stuff that covers LibVersion 10, 11 and 12
{$ELSE}
  {$IFDEF Lib6}
    do stuff that covers LibVersion 6,7,8,9
  {$ENDIF}
{$ENDIF}

It's just a slightly more advanced version of processing the defines.
The {$IF ..} notation is a bit more powerful and it allows you to query constant expressions and not just defines.

The {$IF ..} directive was introduced in Delphi 6.

I guess Embarcadero decided to clean up the code base.