martedì 1 gennaio 2019

Inline Variable Declaration

Inline variable declaration is a new very interesting feature introduced in Delphi language. It's nothing particularly new in the programming languages panorama, but it is new in Delphi's.   
It is the type of upgrade that makes a language modern and flexible.   Some may like it,  others dislike it, in my opinion is just a new useful feature.  
Some thinks this is going to break down Delphi traditional way of writing, and it could may introduce unnecessary complexity or code may appear less clear.
But that doesn't really depends on the new feature: I have seen bad code without it. The truth is, as always, how you use it, and that means... just don't abuse of it! 
Here a sample of new syntax:
   var I: Integer;
   I := 22;  
It looks strange to see it in pascal code, isn't it?  It is not necessary to declare the variable first, in procedure/function var section , but just declare it in code flow.  
It is possible to have declaration and initialization done in a single statement as follow:
  var I: Integer:=22; 

Scope and memory usage

One of the benefit of having inline declaration, can be  also memory usage, as the scope of the variable starts from it's declaration, and it lives inside the block it is declared in. 
Let's have a look to the following code:
   var I: Integer := 22;
   if I > 10 then
     var J: Integer := 3;
     ShowMessage (J.ToString);
     var K: Integer := 3;
     ShowMessage (J.ToString); // COMPILER ERROR: Undeclared identifier
As expected, the variable J is not accessible in the second block!
And what does it happen in case of Intefaces or records declarations? 
  if (something) then
    var Intf: IInterface = GetInterface; // Intf.AddRef
    var MRec: TManagedRecord = GetMRecValue; // MRec.Create + MRec.Assign
  end; // Intf.Release and MRec.Destroy are implicitly called at end of scope

As you can try an see from the example above, as soon as variable goes out of scope, interfaces or record variables are automatically freed.

Type Inference for Inlined Variables

Another benefit gained from introducing inline variables, is the ability to guess the type of the variable we are going to declare:
  var I := 22;

Compiler automatically assign type of integer. Compiler convert the variable to the larger type. In the example, variable type should be shortint. But you can always specify the type if you don't like this rule.
Still, that does not means that you can assign different types to the same variable
  var I := 22;
  I := 'Hello'; //<--- Incompatible type: integer and string
Once, type is assigned to a variable, it lasts for the entire life of the variable, as expected. 
Type inference, comes very handy with complex type definition, avoiding re-writing  types definitions as shown in snippet code:
 var MyDictionary := TDictionary<string, Integer>.Create; //<---
 MyDictionary.Add ('one', 1);
 var APair := MyDictionary.ExtractPair('one'); //<-- Apair: TPair
 ShowMessage (APair.Value.ToString)

For Loops With Variable Declaration

Another useful and powerfull, usage of inline declaration comes with  for loops. I think this is the case I would use it more frequently, I have always hated declaring the for loops variable!!!

 for var I: Integer := 1 to 10 do ...
 for var Item: TItemType in Collection do.

It does works with complex type! Marvelous!!! 

In my opinion, this is a great new simple feature, that will allows us to save up time on typing. Delphi will always be a strongly-typed programming language, that is what we like it best!

If you are interested on going deeper on newly Delphi features, I suggest you, to give a look at bittimeProfessionals courses page, on and on, to get information about upcoming courses. 

Nessun commento:

Posta un commento