Difference between revisions of "$IF"

From Lazarus wiki
Jump to navigationJump to search
m
 
(4 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
{{$IF}}
 
{{$IF}}
  
The $IF directive can be used in [[Conditional compilation|conditional compilation]].
+
The <syntaxhighlight lang="pascal" inline>{$if …}</syntaxhighlight> directive can be used in [[Conditional compilation|conditional compilation]].
  
<code>
+
<syntaxhighlight lang="pascal" highlight="2,5">
{$IF CompilerVersion >= 20 }
+
// pointermath directive did not exist prior FPC 3.0.0
    if FHandle = INVALID_HANDLE_VALUE then
+
{$if FPC_VERSION > 2}
{$IFEND}     
+
// pointer arithmetics is bad. very bad
</code>
+
{$pointermath off}
 +
{$endif}
 +
</syntaxhighlight>
 +
 
 +
Also, enable you to write complex conditions that you cannot do with the <syntaxhighlight lang="pascal" inline>{$ifdef …}</syntaxhighlight>,
 +
 
 +
And there are two bool functions[defined/undefined] that can be mixedup with [and/or] logic operators, For example:
 +
<syntaxhighlight lang="pascal" highlight="2,5">
 +
//Befor you need write these conditions to check some conditions:
 +
{$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 readabl 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}
 +
 
 +
</syntaxhighlight>
  
 
{{Directives, Defines and Conditionals}}
 
{{Directives, Defines and Conditionals}}

Latest revision as of 17:12, 6 August 2022

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}

Also, enable you to write complex conditions that you cannot do with the {$ifdef …},

And there are two bool functions[defined/undefined] that can be mixedup with [and/or] logic operators, For example:

//Befor you need write these conditions to check some conditions:
{$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 readabl 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
$IF