It's simple to use! For example:
- Code: Select all
{$DEFINE RetStr} //If defined our func will return string, else BOOL.
function GetResult:{$IFDEF RetStr} string; {$ELSEIF} boolean; {$ENDIF}
begin
Result:={$IFDEF RetStr} 'It is amazing!1111'; {$ELSEIF} True; {$ENDIF}
end;
Delphi Compiler Pre-processor will ignore code if it included in definition that returns false.
Code above will be pre-processed to this:
- Code: Select all
{
if {$DEFINE RetStr} //RetStr defined
}
//pre-processed code
function GetResult: string;
begin
Result:= 'It is amazing!1111';
end;
{
if //{$DEFINE RetStr} //RetStr not defined
}
//pre-processed code
function GetResult:boolean;
begin
Result:=True;
end;
Now we undef our RetStr.
That the result is:
- Code: Select all
{
if {$DEFINE RetStr} //RetStr defined
}
//pre-processed code
function GetResult: string;
begin
Result:= 'It is amazing!1111';
end;
{
///
}
{$UNDEF RetStr} //RetStr will be return false, it it defined
{
if //{$DEFINE RetStr} //RetStr not defined
}
//pre-processed code
function GetResult:boolean;
begin
Result:=True;
end;
Definitions it's a compiller bool operations on pre-processing code, in delphi it's view like this:
- Code: Select all
var
VarBool:Boolean; //It's equal {$DEFINE VarBool}
begin
if VarBool then //it's Equal {$IFDEF VarBool}
begin
{...}
end else //it's Equal {$ELSEIF}
begin
{...}
end; //it's Equal {$ENDIF}
if not VarBool then //it's Equal {$IFNDEF VarBool}
begin
{...}
end else //it's Equal {$ELSEIF}
begin
{...}
end; //it's Equal {$ENDIF}
end';
With $UNDEF it's view like:
- Code: Select all
[code]
var
VarBool:Boolean; //It's equal {$DEFINE VarBool}
begin
if VarBool then //it's Equal {$IFDEF VarBool}
begin
{...}
end else //it's Equal {$ELSEIF}
begin
{...}
end; //it's Equal {$ENDIF}
VarBool:=FALSE; //it's Equal {$UNDEF RetStr}
if not VarBool then //it's Equal {$IFNDEF VarBool}
begin
{...}
end else //it's Equal {$ELSEIF}
begin
{...}
end; //it's Equal {$ENDIF}
end';
[/code]
Can I do this in any way for compile time?
Compiler automatically pre-processes defenitions in compile-time.