Delphi 中是否有 auto 关键字?
Is there an auto keyword in Delphi?
我正在学习 Delphi 我自己..我已经看到 auto
变量类型可以在 C++ 中做一些神奇的事情。在Delphi中是否有auto
变量类型或类似的东西?
auto
在 C++ 中用于让编译器根据用于初始化变量的值类型推断变量的数据类型。
在 Delphi 10.3 及更高版本中,type inference is available only on an inline variable:
Additionally, the compiler can now in several circumstances infer the type of a variable at its inline declaration location, by looking to the type of the value assigned to it.
procedure Test;
begin
var I := 22;
ShowMessage (I.ToString);
end;
The type of the r-value expression (that is, what comes after the :=
) is analyzed to determine the type of the variable. Some of the data types are “expanded” to a larger type, as in the case above where the numeric value 22 (a ShortInt
) is expanded to Integer
. As a general rule, if the right hand expression type is an integral type and smaller than 32 bits, the variable will be declared as a 32-bit Integer
. You can use an explicit type if you want a specific, smaller, numeric type.
我正在学习 Delphi 我自己..我已经看到 auto
变量类型可以在 C++ 中做一些神奇的事情。在Delphi中是否有auto
变量类型或类似的东西?
auto
在 C++ 中用于让编译器根据用于初始化变量的值类型推断变量的数据类型。
在 Delphi 10.3 及更高版本中,type inference is available only on an inline variable:
Additionally, the compiler can now in several circumstances infer the type of a variable at its inline declaration location, by looking to the type of the value assigned to it.
procedure Test; begin var I := 22; ShowMessage (I.ToString); end;
The type of the r-value expression (that is, what comes after the
:=
) is analyzed to determine the type of the variable. Some of the data types are “expanded” to a larger type, as in the case above where the numeric value 22 (aShortInt
) is expanded toInteger
. As a general rule, if the right hand expression type is an integral type and smaller than 32 bits, the variable will be declared as a 32-bitInteger
. You can use an explicit type if you want a specific, smaller, numeric type.