$IF
From Lazarus wiki
Jump to navigationJump to search
│
Deutsch (de) │
English (en) │
français (fr) │
русский (ru) │
The {$if …}
directive can be used in conditional compilation.
// pointermath directive did not exist prior FPC 3.0.0
{$if FPC_VERSION > 2}
// pointer arithmetics is bad. very bad
{$pointermath off}
{$endif}
This directive enables the use of complex conditions that are not possible with {$ifdef}
.
It also introduces boolean functions such as `defined()` and `undefined()`, which can be combined with logical operators like `and` and `or`. This makes condition checks simpler and more readable. For example:
// Previously, to check multiple conditions, you had to write:
{$define SOMETHING}
{$define SOMETHINGELSE}
{$ifdef SOMETHING}//Union $IfDef to check multiple conditions
{$ifdef SOMETHINGELSE}
{$ModeSwitch advancedrecords}
{$endif}
{$endif}
//But with the {$IF} you can check conditions together:
{$if defined(SOMETHING) and defined(SOMETHINGELSE)}//simple and readable instead of union {$IFDef}`s
{$ModeSwitch advancedrecords}
{$endif}
{$if defined(somthing) or defined(somethingelse)}
//Whatever you need!
{$endif}
{$if undefined(what) and defined(somethingelse)}
//Just for note, Another usage!
{$endif}
Directives, definitions and conditionals definitions |
---|
global compiler directives • local compiler directives Conditional Compiler Options • Conditional compilation • Macros and Conditionals • Platform defines |