I'm migrating a legacy Delphi application to Delphi-XE2, and I'm wondering if there's a good reason to replace the arrays defined as Array of MyType
to TArray<MyType>
. So the question is what are the pros and cons of TArray<T>
usage instead of Array of MyType?
3 Answers
The main advantage is less onerous type identity rules. Consider:
a: array of Integer;
b: array of Integer;
These two variables are not assignment compatible. It is a compiler error to write:
a := b;
On the other hand if you use the generic syntax:
a: TArray<Integer>;
b: TArray<Integer>;
then these two variables are assignment compatible.
Sure, you can write
type
TIntegerArray = array of Integer;
But all parties need to agree on the same type. It's fine if all code is in your control, but when using code from a variety of sources, the advent of generic dynamic arrays makes a huge difference.
The other advantage that springs to mind, in similar vein, is that you can readily use the generic array type as the return type of a generic method.
Without the generic array you are compelled to declare a type of this form:
TArrayOfT = array of T
in your generic class, which is rather messy. And if you are writing a generic method in a non-generic class, then you've no way to make that declaration. Again the generic array solves the problem.
TMyClass = class
class function Foo<T>: TArray<T>; static;
end;
This all follows on from type compatibility rules described in the documentation like this:
Type Compatibility
Two non-instantiated generics are considered assignment compatible only if they are identical or are aliases to a common type.
Two instantiated generics are considered assignment compatible if the base types are identical (or are aliases to a common type) and the type arguments are identical.
-
2What about bloated code with generics? Was the problem solved in XE2/XE3? I would never recommend declare a variable as TArray<T>.– kludgJan 17, 2013 at 16:13
-
7@Serg: That hasn't been fixed, but it doesn't apply here. The problem is that methods get duplicated multiple times, but
TArray<T>
isn't a class, it's an array. So there are no methods to duplicate, and therefore no code bloat. Jan 17, 2013 at 16:18 -
5I very rarely find
TArray<T>
used but it's probably because Delphi developers still mostly write code like it's 1996. It's a conservative approach to ignore new syntax for at least a decade. (grin)– Warren PJan 17, 2013 at 18:21 -
4@Warren Our codebase uses it exclusively for the reasons outlined in my answer. It's a no-brainer in my view. Jan 17, 2013 at 18:31
-
5One important aspect of declaring constant array arguments in methods, use
AMethod(const a : array of T);
instead ofAMethod(const a : TArray<T>);
. The former accepts passing any array of T, also constant expressions like callingAMethod([1,2,3]);
, while the latter only accepts parameters of typeTArray<T>
.– LU RDJun 18, 2013 at 9:12
You can initialize TArray<T>
with values with one construct:
var
LArray: TArray<Integer>;
begin
LArray := TArray<Integer>.Create(1, 2, 3, 4);
For array of Integer
you would need to write much more code:
var
LArray: array of Integer;
begin
SetLength(LArray, 4);
LArray[0] := 1;
LArray[1] := 2;
LArray[2] := 3;
LArray[3] := 4;
-
13
type TMyIntArr = array of integer; var LArray : TMyIntArr; LArray := TMyIntArr.Create(1,2,3,4);
works fine.– LU RDJan 17, 2013 at 16:30 -
5@LURD: +1, and it works in Delphi 2007 as well as in later versions that support generics (and with types other than integer, to make sure that's clear). Jan 17, 2013 at 17:06
-
2@EricGrange, can you exemplify? I disassembled the generic array create and the dynamic array create in XE3 and the compiler emits the same code. Looks ok to me.– LU RDJan 18, 2013 at 7:47
-
1Sorry, for the delay, I don't check answers too often here, check the asm for aig : Tarray<Integer>; ain : array of Integer; begin aig:=TArray<Integer>.Create(1, 2); SetLength(ain, 2); ain[0]:=1; ain[1]:=2; In the first case, you get an array clear, a set length, a wordy assignment for the values, and an array assign, in the second case, you get just a set length and a straight assignment for the values. Feb 15, 2013 at 9:30
-
2@Eric: From reading your article, I think the clarification should be put in here that it might be less performant in some speed-critical situations to use the Create array pseudo-contructor. Having read the above comments, I thought you were saying that it was buggy. Good article, though.– jepJun 24, 2013 at 16:48
It comes in handy for function results.
Example:
The following is not allowed in Delphi. You need to declare a separate type here. What a waste of time.
function MyFunc:array of integer;
begin
end;
Wait, generics to the resque:
function MyFunc:TArray<integer>;
begin
end;
-
8Well, let's be fair, TIntegerDynArray was introduced in what? D4? D6? So you should use an own defined type. Aug 22, 2014 at 8:52