Knowledge Base

Delphi and Inline var declarations

Inline variables have been a game-changer since their introduction in Delphi 10.3. This feature allows you to declare variables directly within your code blocks, moving away from the traditional var blocks. While this might feel unfamiliar to seasoned Delphi developers, the advantages it offers are too significant to ignore.

Scoped declarations

One of the key benefits of inline variables is that they are scoped to the block where they are declared. This means they are only accessible after their declaration and only within their specific code block. This reduces the chances of errors, such as reusing variables unintentionally or dealing with incorrect initializations. Refactoring becomes easier too, as it’s clear which variables need to be carried over when moving code between functions.

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-part from here, you will see automatically (through Code Insight, but otherwise also through compiling) that the variable Customer is unknown. This level of clarity is not achievable with traditional variable declarations.

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

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; 

interface 

type 
  ICustomer = interface 
    ['GUID'] 
    
    function CompanyName: string; 
    function Name: string; 
  end;
unit DataModel.CustomerOrder;   

interface  

uses DataModel.Customer;  
 
type  
  ICustomerOrder = interface  
    ['GUID'] 
     
    function Customer: ICustomer;  
end;

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.

At GDK Software, we encourage adopting modern features like inline variables to improve your Delphi codebase. Whether you’re upgrading an older project or starting fresh, these features can make your code cleaner, safer, and more efficient.

If you need help modernizing your Delphi application or want to explore how inline variables can enhance your projects, contact us at info@gdksoftware.com. Let’s take your Delphi development to the next level!

Contact

Let us help you to realise your ambitions

GDK Software UK

(+44) 20 3355 4470

GDK Software USA

+1 (575) 733-5744