DelphiBasics
$X
Compiler Directive
Controls some Pascal extension handling
1   {$X-}
2   {$X+}
Description
The $X compiler directive determines whether Delphi includes a number of Pascal language extensions or not.
 
This affects three areas:
 
1.Treating functions as procedures
 
The example code shows the before and after effect of $X. When +, Delphi will allow a function result to be ignored - unassigned - thereby treating the function as if it were a procedure.
 
2.Using Result in functions
 
With $X-, you must return a function value by assigning to the pseudo variable of the same name as the function. For example : function GetValue : Integer; begin   GetValue := 23; end;
 
With $X+, you can also assign to the pseudo variable Result as in the example code.
 
3.Treating Char arrays as strings
 
With $X+, a zero base array of Char variables can be assigned a string value. It can also be assigned from, as long as the array has a #0 value element to signify the string end.
Notes
$ExtendedSyntax is equivalent to $X.

The default value is $X+

$X should be set just once in your code.
Related commands
$BoolEval Whether to short cut and and or operations
$ExtendedSyntax Controls some Pascal extension handling
Function Defines a subroutine that returns a value
 Author links

 Buy Website Traffic at
 Buywebsitetrafficexperts.com

 Buy Proxies at
 Buyproxies.io
 
 Download this web site as a Windows program.




 
Example code : Fail to compile code that does not assign a function result
begin
  // Set extended syntax off
  {$X-}

  // Call the GetValue function without assigning the result
  GetValue;
end;

// A function that simply returns the value 1
function TForm1.GetValue: Integer;
begin
  Result := 1;
end;
Show full unit code
   Compiltaion fails :
  
   [Error] Unit1.pas(39): Statement expected, but expression of type 'Integer' found
   [Error] Unit1.pas(45) Undeclared identifier: 'Result'
   [Fatal Error] Project1.dpr(5): Could not compile used unit 'Unit1.pas'
 
Example code : Code that does not assign a function result compiles OK
// Full Unit code.
// -----------------------------------------------------------
// You must store this code in a unit called Unit1 with a form
// called Form1 that has an OnCreate event called FormCreate.
 
unit Unit1;
 
interface
 
uses
  Forms, Dialogs;
 
type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
    function GetValue : Integer;
  end;
 
var
  
Form1: TForm1;
 
implementation
{$R *.dfm} // Include form definitions
 
procedure TForm1.FormCreate(Sender: TObject);

begin
  // Set extended syntax on
  {$X+}

  // Call the GetValue function without assigning the result
  GetValue;
end;

// A function that simply returns the value 1
function TForm1.GetValue: Integer;
begin
  Result := 1;
end;
 
end.
Hide full unit code
   Code compiles OK and runs without doing anything.
 
Delphi Programming Neil Moffatt 2002 - 2017. All rights reserved.  |  Home Page