Inline variable declarations

Top  Previous  Next

New features since Delphi 7 > Inline variable declarations

Since Delphi 10.3 variables can be declared not only at the beginning of a routine, but inline too.


Previously, variables always had to be declared in a var block, either globally or at the head of a routine. . For example:


procedure Test;


  I: Integer;


  I := 22;



Since Delphi 10.3. variables also may be declared inside of a code block, either with an explicit type or with an implicit type:



procedure Test;           procedure Test;   

begin                     begin             

  var I: Integer;           var I := 22;    

  ...                       ...             



Delphi2Cpp converts this to:



void Test()               void TestAuto()   

{                         {                 

  int I = 22;               auto I = 22;    

  ...                       ...             



Similarly, constants can now also be defined inline:




  const C1: Integer = 10;

  const C2 = 10;





  const int C1 = 10;

  auto C2 = 10;



No distinction is made here between C++98 and C++11. Auto variable and constant declarations have to be manually corrected for C++98.



The rules for scoping local variables in C++ are exactly the same as inline variables in Delphi, so Delphi2cpp doesn't need to use any special tricks here. This also applies to variables in for loops:


  var total := 0;

  for var I: Integer := 1 to 10 do

    Inc (total, I);




  auto total = 0;

  for(int I = 1; I <= 10; I++)


    total += I;








This page belongs to the Delphi2Cpp Documentation

Delphi2Cpp home  Content