Knowledge Base

Delphi and Inline var declarations

Since Delphi 10.3, you have the possibility in Delphi to declare inline variables directly in your code. We were not used to this in Delphi, so you may find it a bit messy. But it has quite a few advantages.

Scoped declarations

First, it makes the variables scoped. So they only work in the scope where you declare them. They are also, of course, only available after the moment they are declared. This reduces the chance of errors quite a lot. You are less likely to reuse a variable with the chance of incorrect initialisation. Refactoring also makes it easier to copy code to another function.

When moving code around in a function, you can also immediately see what other initialisations you need to take with you.
In the code example below, you can see how this works. Suppose you move the if Customer.IsNew the part from here, you will see automatically (through Code Insight, but otherwise also through compiling) that the variable Customer is unknown. This is not the case with the classic declaration.

if IsCustomer then
  var Customer := Order.Customer;
  if Customer.IsNew then
    var DisplayName := Customer.CompanyName + Customer.Name;
    SendEmail(Customer, DisplayName);

Type recognition

Another advantage is automatic type recognition. So you no longer need to indicate which type a variable is of. This also reduces the need to include a unit in the file you are working on. Code Insight also recognises the type, so you can just continue working with it.

Suppose you have the following units:

unit DataModel.Customer; 


  ICustomer = interface 
    function CompanyName: string; 
    function Name: string; 
unit DataModel.CustomerOrder;   


uses DataModel.Customer;  
  ICustomerOrder = interface  
    function Customer: ICustomer;  

In the first example, we used this like:

var Customer := Order.Customer; // Returns a ICustomer

When a function returns a specific type (like an interface in this example), you don’t need to define this variable or include the unit.

If you use interfaces, there is one exception to this rule. And that is when you create an object and want to use it as an interfaced variable. Then you have to declare the type as well, otherwise the variable is automatically typed as the `T..` variant.

var Customer: ICustomer := TCustomer.Create;

Easier in loops

The variables you declare for a loop can also be declared directly inline. This is usually better, because you always declare the variable specifically for a loop. Again, it prevents you from using a loop variable after your loop code and potentially creating problems with it.

for var i := o to List.Count - 1 do

for var Customer in MailingList.Customers do

Use in Delphi 10.3

Although inline variable declaration is implemented in Delphi 10.3, Code Insight does not recognise it. However, the compiler does. So in Delphi 10.3, don’t be fooled by the red line below your code, it works there too.

Written by Kees de Kraker


Let us help you to realise your ambitions