Unexpected Benefit of Inline Variables: Conditional Blocks

Inline variables is one of the cool new feature coming in 10.3. The obvious huge use case is loop control variables, but I just discovered another great use case while reviewing some code. 

procedure DoesSomething;
var
  var1, var2: Integer;
begin
  // use var1
  {$IFDEF Something}
  // use var1 & var2
  {$ENDIF Something}
end;

This is a pattern I see a lot, and it generates a hint on var2 being unused based on the current compiler directive status.

[dcc32 Hint] myUnit.pas(123): H2164 Variable 'var2' is declared but never used in 'DoesSomething'

Now there are a number of ways to deal with this with more compiler directives, which is what I’ve done in the past, but I never like adding more compiler directives. It makes the code way more complicated and harder to maintain. Now with Inline Variables I can simplify it, make it easier to maintain, and hande the hint! (all of which makes me so happy!)

procedure DoesSomething;
var
  var1: Integer;
begin
  // use var1
  {$IFDEF Something}
  var var2: Integer;
  // use var1 and var2
  {$ENDIF Something}
end;
Happy dance commencing in T-minus 10 seconds. 

What are some interesting ways you see inline variables benefiting you?

This entry was posted in News, Source Code and tagged , , , . Bookmark the permalink.

3 Responses to Unexpected Benefit of Inline Variables: Conditional Blocks

  1. Frederic Bell says:

    Since you mention it, I use compiler directives as a form of documentation! I have some pretty complicated apps, and I can jump through the code by searching on the compiler directive to find out where certain things happen (or don’t). I can use compiler directives to turn functional features on and off. I wish that Delphi produced pre-processor output, so I can see what my code looks like when I turn certain directives on and off… without the confusion that often results from nested directives. The inline var feature is going to help a little in this regard.

  2. HeartWare says:

    @FredericBell: I have made a program that parses a .PAS source file, gathers up all {$IFDEF}s, {$DEFINE}s and {$UNDEF}s, provides a list of these (allowing you to specify which are on or off), and then parses the file and emits a new .PAS file as the compiler sees it with the selected conditionals. If you would like a copy, I can put it online somewhere… It currently doesn’t handle “{$IF DEFINED … }” or “{$IF CompilerVersion … }” and other “{$IF … }” conditionals, as they require a more sophisticated parser than I have had the need of…

  3. Frederic Bell says:

    Another comment: If you’re like me, you want your apps to be compilable in earlier versions of Delphi too… Using inline variables is only supported on 10.3 (and later, but you never know though), so you end with even more {$ifdef VER330} than you had before! Sorry Jim

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.