Difference between revisions of "$extendedSyntax"

From Lazarus wiki
Jump to navigationJump to search
(use categorization template)
(extend)
Line 5: Line 5:
  
 
== affected syntax ==
 
== affected syntax ==
* [[Function|Functions]] can be called as if they were [[Procedure|procedures]]. The function result is discarded. This is potentially harmful if, for example, the function allocated new memory space and returned a pointer to it.
+
* [[Function|Functions]] can be called as if they were [[Procedure|procedures]]. The function result is discarded. This is potentially harmful if, for example, the function allocated new memory space and returned a pointer to it. Nonetheless, ''managed data types'' are insusceptible to leakage. Implementing a [[management operators|management operator]] can turn any <syntaxhighlight lang="pascal" inline>record</syntaxhighlight> into a managed data type.
* Integer ''arithmetic'' operations are allowed on [[Pointer|pointers]]. The directive [[$pointerMath|<syntaxhighlight lang="pascal" inline>{$pointerMath}</syntaxhighlight>]] had to be ''on'' for that ''during'' the respective pointer type’s definition.
+
* Integer ''arithmetic'' expressions are allowed on [[Pointer|pointers]]. The directive [[$pointerMath|<syntaxhighlight lang="pascal" inline>{$pointerMath}</syntaxhighlight>]] had to be ''on'' for that ''during'' the respective pointer type’s definition.
 
* Pointers become ''ordered'' and can be compared using [[Less than|<syntaxhighlight lang="pascal" inline><</syntaxhighlight>]],[[Greater than|<syntaxhighlight lang="pascal" inline>></syntaxhighlight>]], <syntaxhighlight lang="pascal" inline><=</syntaxhighlight> and <syntaxhighlight lang="pascal" inline>>=</syntaxhighlight>. ''Typed'' pointers have to correspond to each other. The [[Equal|<syntaxhighlight lang="pascal" inline>=</syntaxhighlight>]] and [[Not equal|<syntaxhighlight lang="pascal" inline><></syntaxhighlight>]] comparisons work ''regardless'' of the <syntaxhighlight lang="pascal" inline>{$extendedSyntax}</syntaxhighlight> state.
 
* Pointers become ''ordered'' and can be compared using [[Less than|<syntaxhighlight lang="pascal" inline><</syntaxhighlight>]],[[Greater than|<syntaxhighlight lang="pascal" inline>></syntaxhighlight>]], <syntaxhighlight lang="pascal" inline><=</syntaxhighlight> and <syntaxhighlight lang="pascal" inline>>=</syntaxhighlight>. ''Typed'' pointers have to correspond to each other. The [[Equal|<syntaxhighlight lang="pascal" inline>=</syntaxhighlight>]] and [[Not equal|<syntaxhighlight lang="pascal" inline><></syntaxhighlight>]] comparisons work ''regardless'' of the <syntaxhighlight lang="pascal" inline>{$extendedSyntax}</syntaxhighlight> state.
 +
 +
== notes ==
 +
* If you have <syntaxhighlight lang="pascal" inline>{$extendedSyntax off}</syntaxhighlight>, you can still do pointer arithmetic with routines from other units if they have been compiled with <syntaxhighlight lang="pascal" inline>{$extendedSyntax on}</syntaxhighlight>, for example [[Inc and Dec#Special behaviors|<syntaxhighlight lang="pascal" inline>inc</syntaxhighlight> and <syntaxhighlight lang="pascal" inline>dec</syntaxhighlight>]]:<syntaxhighlight lang="pascal" highlight="2,7">program pointerMathDemo(input, output, stdErr);
 +
{$extendedSyntax off}
 +
var
 +
p: pChar;
 +
begin
 +
p := nil;
 +
inc(p, 42); { no problem }
 +
end.</syntaxhighlight>Similarly, unusual comparison operations may be accessible with foreign routines.
 +
* Although with <syntaxhighlight lang="pascal" inline>{$extendedSyntax on}</syntaxhighlight> pointers become ordered, they do ''not'' become ''ordinal data types''; the standard functions [[Ord|<syntaxhighlight lang="pascal" inline>ord</syntaxhighlight>]], <syntaxhighlight lang="pascal" inline>succ</syntaxhighlight> and <syntaxhighlight lang="pascal" inline>pred</syntaxhighlight> are still not applicable on pointers.
  
 
== comparative remarks ==
 
== comparative remarks ==

Revision as of 02:58, 17 January 2022

Deutsch (de) English (en)


The global compiler directive {$extendedSyntax on} turns on additional syntax. The FPC has this by default on. The short notation is {$X+}/{$X‑}.

affected syntax

  • Functions can be called as if they were procedures. The function result is discarded. This is potentially harmful if, for example, the function allocated new memory space and returned a pointer to it. Nonetheless, managed data types are insusceptible to leakage. Implementing a management operator can turn any record into a managed data type.
  • Integer arithmetic expressions are allowed on pointers. The directive {$pointerMath} had to be on for that during the respective pointer type’s definition.
  • Pointers become ordered and can be compared using <,>, <= and >=. Typed pointers have to correspond to each other. The = and <> comparisons work regardless of the {$extendedSyntax} state.

notes

  • If you have {$extendedSyntax off}, you can still do pointer arithmetic with routines from other units if they have been compiled with {$extendedSyntax on}, for example inc and dec:
    program pointerMathDemo(input, output, stdErr);
    {$extendedSyntax off}
    var
    	p: pChar;
    begin
    	p := nil;
    	inc(p, 42); { no problem }
    end.
    
    Similarly, unusual comparison operations may be accessible with foreign routines.
  • Although with {$extendedSyntax on} pointers become ordered, they do not become ordinal data types; the standard functions ord, succ and pred are still not applicable on pointers.

comparative remarks

see also