There is a significant new feature in the Object Pascal language for XE7 and that is the improved support for initializing dynamic arrays and operating on them. The What's new page covers is here: docwiki.embarcadero.com/RADStudio/XE7/en/What%27s_New_in_Delphi_and_C%2B%2BBuilder_XE7#String-Like_Operations_Supported_on_Dynamic_Arrays.
Before we start, if you haven't used dynamic arrays in object Pascal much, I can recommend you this nice introduction by Jacob (of Castalia fame): delphiblog.twodesk.com/dynamic-arrays-how-they-work-and-how-to-use-them
So what's new in XE7? The Object Pascal language allows you to do a few new operations:
- Assign a constant array to a dynamic array
- Concatenate two constant arrays with the + symbol
- Use the following RTL functions on any dynamic array (not just strings): Insert, Delete, and Concat
You might have seen code like this, that we used in some demos:
var di: array of Integer; i: Integer; begin di := [1, 2, 3]; // initialization di := di + di; // concatenation di := di + [4, 5]; // mixed concatenation for i in di do begin Memo1.Lines.Add (i.ToString); end;
Notice the use of a for-in statement to scan the array elements. In terms of using the RTL functions, you can now write code like the following:
var di: array of Integer; i: Integer; begin di := [1, 2, 3, 4, 5, 6]; Insert ([8, 9], di, 4); Delete (di, 2, 1); // remove the third (0-based)
Again, this demo has been floating around, so you might have already seen it. Now, the Integer type is only one of the types you can use in a dynamic array. In fact, you can use ANY data type, including managed types (for example, a dynamic array of interfaces initialized using objects as Daniele Teti was showing me the other day with an array of ITask elements from the parallel library). Another not so obvious example? What about an array of buttons? Of course it was possible to create one even before, but not to write it this way:
var buttons: array of TButton; aButton: TButton; begin buttons := [btnArrayInit, btnArrayRTL, btnButtonsArray]; for aButton in buttons do begin aButton.Caption := aButton.Caption + '*'; end; buttons := buttons + [TButton.Create(self)];
This snippet highlights the fact you can use this feature in countless different scenarios. Arrays of interfaces, records, objects, native types... just beware that if the type is not managed, you'll have to delete the actual elements when the array is disposed, at least on non ARC-enabled platforms.
Happy coding in XE7... and if still don't have it, take advantage of the 10% discount available until the end of September: http://www.embarcadero.com/radoffer