I'm learning Delphi myself.. I have seen the auto variable type that can do some degree of magic in C . Is there an auto a variable type, or something similar to this, in Delphi?
CodePudding user response:
auto in C is used to let the compiler infer a variable's data type based on what type of value is used to initialize it.
In Delphi 10.3 and later, 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.
