Inline variables in the graphic version of Delphi basic tutorial

Inline variables are a feature introduced in Delphi Rio 10.3. This feature is actually not new in other languages. When I first encountered Delphi, if I had to complain about it, it would be the declaration of variables.

Official document: http://docwiki.embarcadero.com/RADStudio/Rio/en/Inline_Variable_Declaration

Inline variable declaration

Previously, local variables needed to be declared in the following way

procedure Test;
var
  I: Integer;
begin
  I := 22;
  ShowMessage (I.ToString);
end;

Now use inline variables to declare

procedure Test;
begin
  var I: Integer;
  I := 22;
  ShowMessage (I.ToString);
end;

Two points about the first type of variable declaration

(1) Variables like I may only be used temporarily, but it still needs to be declared in advance. Some friends may say that this syntax is more readable, but when the amount of code is large, drag the mouse repeatedly to check The type of statement is also very troublesome, but I feel that the readability is reduced

(2) The scope of the variable, as long as the variable is declared, its scope is the entire function and process, but sometimes it is only necessary to use the variable in the loop or if statement, which invisibly causes a waste of resources

Reference reading

  • Inline variables can improve performance: https://blog.grijjy.com/2018/11/02/inline-variables-can-increase-performance/

Variable range

Here directly refer to the code example given in the official document

procedure Test; // 在单个语句中声明和初始化
begin
  var I: Integer := 22;
  ShowMessage (I.ToString);
end;

procedure Test1; // 多个内联声明
begin
  var I: Integer := 22;
  var J: Integer;
  J := 22 + I;
  var K: Integer := I + J;
  ShowMessage (K.ToString);
end;

procedure Test2; // 范围仅限于本语句块
begin
  var I: Integer := 22;
  if I > 10 then
  begin
    var J: Integer := 3;
    ShowMessage (J.ToString);
  end
  else
  begin
    var K: Integer := 3;
    ShowMessage (J.ToString); // COMPILER ERROR: “Undeclared identifier: ‘J’”
  end;
end;

In the process of Test2, because the two variables J and K are declared in the corresponding statement block, they cannot be accessed when they exceed the scope of the f statement. Similarly, J is declared in the if block in the else Inaccessible within the block

Type inference

When using some unfamiliar APIs, you need to repeatedly check the return value type of the function when defining the variable that receives the return value of the function. I don’t know if everyone feels it, anyway, I’m irritated. Fortunately, the last feature of inline variables is that they can be type inferred (or called type inference). See the code below for specific things

procedure NewTest;
begin
  var MyDictionary := TDictionary<string, Integer>.Create;
  MyDictionary.Add ('one', 1);
  var APair := MyDictionary.ExtractPair('one');
  ShowMessage (APair.Value.ToString)
end;

The two variables MyDictionary and APair do not need to declare the type when receiving their return value. According to the function on the right side of the equal sign, the type can be automatically deduced. This is type inference

Of course, although the type definition is omitted when declaring variables, you still need to know what it represents

Use of inline variables in loops

Personally, this is the most convenient place for inline variables. No nonsense, code on Cuihua...

  for var I: Integer := 1 to 10 do
      ...

  for var Item: TItemType in Collection do
      ...

At this point, the introduction of inline variables is all over, but it should be noted that although the official announcement is that inline variables are supported in Delphi Rio 10.3, the actual situation is that the IDE has BUG. When you use it in this version Sometimes there will be annoying red lines although the compilation can pass. This issue has been fixed in Delphi Sydney 10.4

Guess you like

Origin blog.csdn.net/farmer_city/article/details/109488310