When writing some unit tests a while ago I found myself needing to initialise some dynamic arrays with test data. It would be nice if we could do something like this: var A : array of Integer ; begin A := ( 1 , 2 , 3 , 4 ) ; // !! WRONG end ; but we can't. So I decided to write some functions to initialise dynamic arrays to the contents of another array, be it a constant, a literal or another dynamic array. The result was a set of overloaded routines, one for each data type I needed to handle, for example for Integer and string arrays I had: function CloneArray ( const A : array of Integer ) : TIntegerDynArray ; overload ; var Idx : Integer ; begin SetLength ( Result , Length ( A ) ) ; for Idx := Low ( A ) to High ( A ) do Result [ Idx - Low ( A ) ] := A [ Idx ] ; end ; function CloneArray ( const A : array of string ) : TStringDynArray ; overload ; var Idx : Integer ; begin SetLength ( Result , Len...
Yesterday I added a shiny new unit to the DelphiDabbler Code Library - the Array Utilities Unit . It lives in the ddablib/arrayutils repo on GitHub. This unit, named DelphiDabbler.Lib.ArrayUtils.pas , provides a single "Advanced" record, TArrayUtils , that contains static methods that perform common operations on generic arrays. There are numerous overloaded versions of many of the methods. Every method operates on a given generic array that is passed as a parameter, either as an array of T or a TArray<T> . The unit requires Delphi XE or later. It has been tested with Delphi XE and Delphi 12. The code depends only on the Delphi RTL, so it should work for any platform supported by Delphi and be both VCL and FireMonkey compatible. I've only tested Windows 32 bit and 64 bit targets. There are far too many methods to describe in here, but you can find them all listed and explained in the comprehensive online documentation . As a taster you'll find Methods that ...
The new feature currently planned for release in the forthcoming Delphi 13 that I'm most excited about is the inline if statement. According to Marco Cantu's blog post , the new syntax is like this: X := if Left < 100 then 22 else 45; Another use is something like this: ShowMessage(if Odd(X) then 'odd' else 'even'); The values returned from the then and else parts must be of the same or compatible type. Apart from being really chuffed that this feature is being added I'm just as happy that the syntax is Pascal like, and is therefore more readable than the cryptic C style ?: construct. Also pleasing is the fact that Embarcadero have gone with version number 13 instead of being ridiculously superstitious! Since Delphi 13 has yet to be released the usual disclaimer applies that this feature is not guaranteed to be included.
Comments
Post a Comment
Comments are very welcome, but please be aware that I moderate all comments, so there will be a delay before your comment appears.
Advertising spam and the rare abusive, hateful or racist comments will be blocked and reported.
Finally, should you have a query about, or a bug report for, one of my programs or libraries please use the relevant issue tracker rather than posting a comment to report it.
Thanks