The basic usage of compile time expressions is as follows:
{$if expr}
CompileTheseLines;
{$endif}
If expr evaluates to TRUE, then CompileTheseLines will be included in the source.
Like in regular pascal, it is possible to use {$ELSE }:
{$if expr}
CompileTheseLines;
{$else}
BetterCompileTheseLines;
{$endif}
If expr evaluates to True, CompileTheseLines will be compiled. Otherwise, BetterCompileTheseLines
will be compiled.
Additionally, it is possible to use {$ELSEIF}
{$IF expr}
// ...
{$ELSEIF expr}
// ...
{$ELSEIF expr}
// ...
{$ELSE}
// ...
{$ENDIF}
In addition to the above constructs, which are also supported by Delphi, the above is completely
equivalent to the following construct in MacPas mode:
{$IFC expr}
//...
{$ELIFC expr}
...
{$ELIFC expr}
...
{$ELSEC}
...
{$ENDC}
that is, IFC corresponds to IF, ELIFC corresponds to ELSEIF, ELSEC is equivalent with ELSE and
ENDC is the equivalent of ENDIF. Additionally, IFEND is equivalent to ENDIF:
{$IF EXPR}
CompileThis;
{$ENDIF}
In MacPas mode it is possible to mix these constructs.
The following example shows some of the possibilities:
{$ifdef fpc}
var
y : longint;
{$else fpc}
var
z : longint;
{$endif fpc}
var
x : longint;
begin
{$IF (FPC_VERSION > 2) or
((FPC_VERSION = 2)
and ((FPC_RELEASE > 0) or
((FPC_RELEASE = 0) and (FPC_PATCH >= 1))))}
{$DEFINE FPC_VER_201_PLUS}
{$ENDIF}
{$ifdef FPC_VER_201_PLUS}
{$info At least this is version 2.0.1}
{$else}
{$fatal Problem with version check}
{$endif}
{$define x:=1234}
{$if x=1234}
{$info x=1234}
{$else}
{$fatal x should be 1234}
{$endif}
{$if 12asdf and 12asdf}
{$info $if 12asdf and 12asdf is ok}
{$else}
{$fatal $if 12asdf and 12asdf rejected}
{$endif}
{$if 0 or 1}
{$info $if 0 or 1 is ok}
{$else}
{$fatal $if 0 or 1 rejected}
{$endif}
{$if 0}
{$fatal $if 0 accepted}
{$else}
{$info $if 0 is ok}
{$endif}
{$if 12=12}
{$info $if 12=12 is ok}
{$else}
{$fatal $if 12=12 rejected}
{$endif}
{$if 12<>312}
{$info $if 12<>312 is ok}
{$else}
{$fatal $if 12<>312 rejected}
{$endif}
{$if 12<=312}
{$info $if 12<=312 is ok}
{$else}
{$fatal $if 12<=312 rejected}
{$endif}
{$if 12<312}
{$info $if 12<312 is ok}
{$else}
{$fatal $if 12<312 rejected}
{$endif}
{$if a12=a12}
{$info $if a12=a12 is ok}
{$else}
{$fatal $if a12=a12 rejected}
{$endif}
{$if a12<=z312}
{$info $if a12<=z312 is ok}
{$else}
{$fatal $if a12<=z312 rejected}
{$endif}
{$if a12<z312}
{$info $if a12<z312 is ok}
{$else}
{$fatal $if a12<z312 rejected}
{$endif}
{$if not(0)}
{$info $if not(0) is OK}
{$else}
{$fatal $if not(0) rejected}
{$endif}
{$IF NOT UNDEFINED FPC}
// Detect FPC stuff when compiling on MAC.
{$SETC TARGET_RT_MAC_68881:= FALSE}
{$SETC TARGET_OS_MAC := (NOT UNDEFINED MACOS)
OR (NOT UNDEFINED DARWIN)}
{$SETC TARGET_OS_WIN32 := NOT UNDEFINED WIN32}
{$SETC TARGET_OS_UNIX := (NOT UNDEFINED UNIX)
AND (UNDEFINED DARWIN)}
{$SETC TYPE_EXTENDED := TRUE}
{$SETC TYPE_LONGLONG := FALSE}
{$SETC TYPE_BOOL := FALSE}
{$ENDIF}
{$info *************************************************}
{$info * Now have to follow at least 2 error messages: *}
{$info *************************************************}
{$if not(0}
{$endif}
{$if not(<}
{$endif}
end.
As you can see from the example, this construct isn’t useful when used with normal symbols, only
if you use macros, which are explained in section 2.2, page 292. They can be very useful. When
trying this example, you must switch on macro support, with the -Sm command line
switch.
The following example works only in MacPas mode:
{$SETC TARGET_OS_MAC := (NOT UNDEFINED MACOS) OR (NOT UNDEFINED DARWIN)}
{$SETC DEBUG := TRUE}
{$SETC VERSION := 4}
{$SETC NEWMODULEUNDERDEVELOPMENT := (VERSION >= 4) OR DEBUG}
{$IFC NEWMODULEUNDERDEVELOPMENT}
{$IFC TARGET_OS_MAC}
... new mac code
{$ELSEC}
... new other code
{$ENDC}
{$ELSEC}
... old code
{$ENDC}