Difference between revisions of "$extendedSyntax"

From Lazarus wiki
Jump to navigationJump to search
(remove wrong statement)
(→‎Notes: add another caveat: cannot call built-in functions and discard result)
Line 18: Line 18:
 
inc(p, 42); { no problem }
 
inc(p, 42); { no problem }
 
end.</syntaxhighlight>Similarly, unusual comparison operations may be accessible with foreign routines.
 
end.</syntaxhighlight>Similarly, unusual comparison operations may be accessible with foreign routines.
 +
* Built-in functions can never be called as if they procedures<syntaxhighlight lang="pascal" highlight="3">program discardFunctionResultDemo;
 +
{$extendedSyntax on}
 +
begin
 +
pi; { discardFunctionResultDemo.pas(4,4) Error: Illegal expression }
 +
end.</syntaxhighlight>even though a custom {{Doc|package=RTL|unit=system|identifier=pi|text=<syntaxhighlight lang="pascal" inline>pi</syntaxhighlight> <syntaxhighlight lang="pascal" inline>function</syntaxhighlight>}} doing exactly the same would be acceptable.
 
* 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.
 
* 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.
  

Revision as of 11:00, 23 October 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.
  • Built-in functions can never be called as if they procedures
    program discardFunctionResultDemo;
    {$extendedSyntax on}
    begin
    	pi; { discardFunctionResultDemo.pas(4,4) Error: Illegal expression }
    end.
    
    even though a custom pi function doing exactly the same would be acceptable.
  • 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