I'm trying to conditionally assign a local open array variable in order it points to a const open array parameter:
uses
System.Generics.Collections,
System.Generics.Defaults;
type
TArray = class(System.Generics.Collections.TArray)
public
class function SameValues<T>(const AValuesA: array of T; AValuesB: array of T; const AComparer: IEqualityComparer<T>; AOrderMatters : Boolean = True) : Boolean; overload; static;
end;
...
class function TArray.SameValues<T>(const AValuesA: array of T; AValuesB: array of T; const AComparer: IEqualityComparer<T>; AOrderMatters : Boolean = True) : Boolean;
var
ArrA : TArray<T>;
ArrB : TArray<T>;
i : integer;
begin
//checking sizes
if(Length(AValuesA) <> Length(AValuesB)) then
begin
Result := False;
Exit;
end;
if(AOrderMatters) then
begin
//I don't need to change the arrays, so I could directly point to the open array parameters
ArrA := AValuesA;
ArrB := AValuesB;
end else
begin
//copying to local arrays
SetLength(ArrA, Length(AValuesA));
TArray.Copy<T>(AValuesA, ArrA, Length(AValuesA));
SetLength(ArrB, Length(AValuesB));
TArray.Copy<T>(AValuesB, ArrB, Length(AValuesB));
//sorting local arrays
TArray.Sort<T>(ArrA);
TArray.Sort<T>(ArrB);
end;
//comparing elements
i := 0;
while(i < Length(ArrA)) do
begin
if(not AComparer.Equals(ArrA[i], ArrB[i])) then
begin
Result := False;
Exit;
end;
Inc(i);
end;
Result := True;
end;
On compiling, it raises E2010 error at:
//I don't need to change the arrays, so I could directly point to the open array parameters
ArrA := AValuesA;
ArrB := AValuesB;
E2010 Incompatible types: 'Dynamic array' and 'array of T'
I've tried the following:
ArrA := #AValuesA;
ArrB := #AValuesB;
It compiles but then it raises AV exception at runtime (and most of all, I don't know if it would be a safe approach).
Here is my test application code:
uses
System.Generics.Defaults;
procedure TForm1.FormCreate(Sender: TObject);
var
ArrA : array of string;
ArrB : array of string;
begin
SetLength(ArrA, 2);
ArrA[0] := 'hello';
ArrA[1] := 'world';
SetLength(ArrB, 2);
ArrB[0] := 'world';
ArrB[1] := 'hello';
if(TArray.SameValues<string>(ArrA, ArrB, TEqualityComparer<string>.Default, True)) then
ShowMessage('Same values and same order')
else if(TArray.SameValues<string>(ArrA, ArrB, TEqualityComparer<string>.Default, False)) then
ShowMessage('Same values but different order')
else
ShowMessage('Different values');
end;
What you are trying to do is not possible because open arrays and dynamic arrays are not compatible in the way that your code attempts.
There is an easy way to achieve what you want though, using a simple recursion. Like this:
class function TArray.SameValues<T>(const AValuesA: array of T; AValuesB: array of T; const AComparer: IEqualityComparer<T>; AOrderMatters : Boolean = True) : Boolean;
var
ArrA : TArray<T>;
ArrB : TArray<T>;
i : integer;
begin
//checking sizes
if Length(AValuesA) <> Length(AValuesB) then
begin
Result := False;
Exit;
end;
if not AOrderMatters then
begin
//copying to local arrays
SetLength(ArrA, Length(AValuesA));
TArray.Copy<T>(AValuesA, ArrA, Length(AValuesA));
SetLength(ArrB, Length(AValuesB));
TArray.Copy<T>(AValuesB, ArrB, Length(AValuesB));
//sorting local arrays
TArray.Sort<T>(ArrA);
TArray.Sort<T>(ArrB);
Result := SameValues<T>(ArrA, ArrB, AComparer, True);
Exit;
end;
//comparing elements
for i := 0 to High(AValuesA) do
begin
if not AComparer.Equals(AValuesA[i], AValuesB[i]) then
begin
Result := False;
Exit;
end;
end;
Result := True;
end;
I'm trying to assign a dynamic array type to a TArray<string> variable
type
TMyStringArray = array of string;
function Test() : TMyStringArray;
begin
...
end;
...
var
MyArray : TArray<string>;
begin
MyArray := Test();
end;
On compiling, Delphi says:
[dcc32 Error] Unit1.pas(39): E2010 Incompatible types:
'System.TArray' and 'TMyStringArray'
I did it simply by using a type cast and it seems to work.
I would be glad to know I can fall into some problems doing this way
type
TMyStringArray = array of string;
function Test() : TMyStringArray;
begin
SetLength(Result, 2);
Result[0] := 'Hello';
Result[1] := 'World';
end;
procedure TForm1.FormCreate(Sender: TObject);
var
MyArray : TArray<string>;
i : integer;
begin
MyArray := TArray<string>(Test());
i := 0;
while(i < Length(MyArray)) do
begin
ShowMessage(MyArray[i]);
Inc(i);
end;
end;
How would I code this so that if an element equals a certain value it would display a message, but if ALL elements inside that array aren't equal to that value, then it would output 'None'?
I've tried
for i := 0 to high(array) do
begin
if (array[i].arrayElement = value) then
begin
WriteLn('A message');
end;
end;
That bit works, but I don't know how to do the check all bit. I had this:
if (array[i].arrayElement to array[high(array)].arrayElement <> value) then
begin
WriteLn('None');
end;
But it didn't allow me to use "to"
It's clearest to write a helper function for this:
function ArrayContains(const arr: array of Integer; const value: Integer): Boolean;
var
i: Integer;
begin
for i := Low(arr) to High(arr) do
if arr[i] = value then
begin
Result := True;
Exit;
end;
Result := False;
end;
Or using for/in:
function ArrayContains(const arr: array of Integer; const value: Integer): Boolean;
var
item: Integer;
begin
for item in arr do
if item = value then
begin
Result := True;
Exit;
end;
Result := False;
end;
Then you call it like this:
if not ArrayContains(myArray, myValue) then
Writeln('value not found');
I have defined a dynamic array type as follows:
TMyIntegerArray = array of integer:
I would like to use an IndexOf function as I would do if it were a TObject's descendant:
var
MyArray : TMyIntegerArray;
i : integer:
begin
//...
i := MyArray.IndexOf(10);
//...
end;
At the moment, the only solution I've found is to write a function who accepts the array and the target value as parameters:
function IndexOf(AArray : TMyIntegerArray; ATargetValue : integer; AOffset : integer = 0);
begin
Result := AOffset;
while(Result < Length(AArray)) do
begin
if(AArray[Result] = ATargetValue)
then Exit;
Result := Result + 1;
end;
Result := -1;
end;
Can TMyIntegerArray type have a function like IndexOf?
Further Informations:
Currently, I'm using Delphi2007 but I'm also interested in knowing if there's any way to add methods to array types in newer Delphi's versions.
In newer versions of Delphi (XE3+), it is possible to implement methods to array types with record helpers:
program ProjectTest;
{$APPTYPE CONSOLE}
Type
TMyArray = array of integer;
TMyArrayHelper = record helper for TMyArray
procedure Print;
function IndexOf(ATargetValue : integer; AOffset : integer = 0): Integer;
end;
procedure TMyArrayHelper.Print;
var
i: Integer;
begin
for i in Self do WriteLn(i); // Use Self for variable reference
end;
function TMyArrayHelper.IndexOf(ATargetValue : integer; AOffset : integer = 0): Integer;
begin
Result := AOffset;
while(Result < Length(Self)) do
begin
if(Self[Result] = ATargetValue)
then Exit;
Result := Result + 1;
end;
Result := -1;
end;
var
myArr : TMyArray;
begin
myArr := [0,1,2]; // A neat way to populate a dynamic array (XE7+)
myArr.Print;
WriteLn(myArr.IndexOf(2));
ReadLn;
end.
Note: You can skip the TMyArray type declaration and use TArray<Integer> for a more relaxed type resolution. As always with record helpers, there can be only one helper attached to a type (and the one that will be used is the one nearest in scope).
This type of helper is called an intrinsic type helper, where the compiler puts an implicit record structure around the type.
Although LU RD showed a direct solution to your question, I am going to add a slightly different approach based on generics. This has the advantage of providing a valid solution for different array types in one place.
For Delphi versions which supports generics, one can adopt the way used in TArray found in System.Generics.Collections. This is a straight forward extension of that class introducing a function IndexOf:
type
TArrayExt = class(TArray)
public
class function IndexOf<T>(const Values: array of T; const Item: T; const Comparer: IEqualityComparer<T>; Index, Count:
Integer): Integer; overload; static;
class function IndexOf<T>(const Values: array of T; const Item: T; const Comparer: IEqualityComparer<T>): Integer; overload;
static;
class function IndexOf<T>(const Values: array of T; const Item: T): Integer; overload; static;
end;
class function TArrayExt.IndexOf<T>(const Values: array of T; const Item: T; const Comparer: IEqualityComparer<T>; Index,
Count: Integer): Integer;
var
I: Integer;
begin
if (Index < Low(Values)) or ((Index > High(Values)) and (Count > 0))
or (Index + Count - 1 > High(Values)) or (Count < 0)
or (Index + Count < 0) then
raise EArgumentOutOfRangeException.CreateRes(#SArgumentOutOfRange);
if Count = 0 then
begin
Exit(-1);
end;
for I := Index to Index + Count - 1 do begin
if Comparer.Equals(Item, Values[I]) then begin
Exit(I);
end;
end;
Result := -1;
end;
class function TArrayExt.IndexOf<T>(const Values: array of T; const Item: T; const Comparer: IEqualityComparer<T>): Integer;
begin
Result := IndexOf<T>(Values, Item, Comparer, Low(Values), Length(Values));
end;
class function TArrayExt.IndexOf<T>(const Values: array of T; const Item: T): Integer;
begin
result := IndexOf<T>(Values, Item, TEqualityComparer<T>.Default, Low(Values), Length(Values));
end;
A simple use case could look like this:
procedure Main;
var
arr: TArray<Integer>;
N: Integer;
begin
arr := TArray<Integer>.Create(5, 7, 3, 4, 2);
repeat
Readln(N);
N := TArrayExt.IndexOf(arr, N);
Writeln(N);
until false;
end;
For Delphi 2009 and above you can use a generic list:
uses System.Generics.Collections;
var
MyArray : TList<integer>;
i : integer;
begin
MyArray := TList<integer>.Create;
MyArray.Add(3);
MyArray.Add(7);
MyArray.Add(10);
i := MyArray.IndexOf(10);
end;
In Delphi 2007 you might use a custom record:
type
TMyArray = record
private
TheArray : array of integer;
public
procedure Add(Value : integer);
function IndexOf(Value : integer) : integer;
function Length : integer;
end;
procedure TMyArray.Add(Value : integer);
var
i : integer;
begin
i := length(TheArray);
setlength(TheArray,i+1);
TheArray[i] := Value;
end;
function TMyArray.IndexOf(Value : integer) : integer;
var
i : integer;
begin
for i := 0 to length(TheArray)-1 do
begin
if TheArray[i] = Value then
begin
Result := i;
exit;
end;
end;
Result := -1;
end;
function TMyArray.Length : integer;
begin
Result := length(TheArray);
end;
procedure MyFunction;
var
MyArray : TMyArray;
i : integer;
begin
MyArray.Add(3);
MyArray.Add(7);
MyArray.Add(10);
i := MyArray.IndexOf(10);
end;
The compiler allows me to do the following:
procedure MyProc(const ADynData: array of string);
or
procedure MyProc(const ADynData: TStringDynArray);
and pass arbitrary data like so:
MyProc(['Data1', 'Data2']);
However, won't allow
function MyFunc: TStringDynArray;
....
function MyFunc: TStringDynArray;
begin
Result := ['Data1', 'Data2'];
end;
or
function MyFunc: TStringDynArray;
const
CDynData: array[0..1] of string = ('Data1', 'Data2');
begin
Result := CDynData;
end;
Why is this? Isn't this technically the same thing?
For these particular scenarios what is the recommended (and most efficient) way of returning an arbitrary array of string?
No, it's not the same thing. In
procedure MyProc(const ADynData: array of string);
the argument is an open array parameter, which is not the same thing as an 'ordinary' dynamic array. The [..] syntax can only be used to create open arrays in open array parameters of functions. (Otherwise, [..] is used to specify sets in code, such as Font.Style := [fsBold, fsItalic]. But sets can only have ordinal types as their 'base types', so there is still no such thing as 'set of string'.)
In other words, it is not possible to write a dynamic array in code like you try in your second code snippet,
function MyFunc: TStringDynArray;
begin
result := ['Data1', 'Data2']; // Won't work.
end;
However, in new versions of Delphi, it is almost possible:
type
TStringDynArray = array of string;
function MyFunc: TStringDynArray;
begin
result := TStringDynArray.Create('A', 'B');
end;
Finally,
function MyFunc: TStringDynArray;
const
CDynData: array[0..1] of string = ('Data1', 'Data2');
begin
result := CDynData;
end;
won't work because TStringDynArray is a dynamic array, while CDynData is a static array, which are two different fundamental types.
This construct
['string1', 'string2']
is known as an open array constructor. From the documentation:
Open array constructors allow you to construct arrays directly within function and procedure calls.
They can be passed only as open array parameters or variant open array parameters.
So, you cannot use an open array constructor to create a function return value.
If you have a fixed number of elements in the array that you need to return, you can use a dynamic array constructor:
Result := TStringDynArray.Create('string1', 'string2');
However, this will not work for a variable number of elements. Now, I know that the example in your question only has a constant number of elements in the array. But I'm sure you'll encounter situations where you need more flexibility than a dynamic array constructor can provide.
If you wish to create a copy of an existing dynamic array and return that, use Copy.
Result := Copy(SomeOtherDynamicArray);
This breaks down when you have an open array at hand. You cannot pass an open array to Copy. Personally I think this is rather a shame since open array parameters are so exceptionally flexible and useful that I'd like to see as much RTL support for them as possible.
So, you end up having to write helper functions for those situations. You can write a dedicated helper for each array type, but that becomes somewhat tiresome. That's where generics come in handy. I have a helper class for the purpose. Here's the relevant extract:
type
TArray = class(Generics.Collections.TArray)
....
class function Copy<T>(const Source: array of T): TArray<T>; overload; static;
....
end;
class function TArray.Copy<T>(const Source: array of T): TArray<T>;
var
i: Integer;
begin
SetLength(Result, Length(Source));
for i := 0 to high(Result) do begin
Result[i] := Source[i];
end;
end;
Now, this works with your string arrays, but also with any other type. Call it like this:
Result := TArray.Copy<string>(SomeStringOpenArray);
A critical point to make is that we are using the generic version of the dynamic array, TArray<string> rather than TStringDynArray. It's essential that you do that if you want to use generics seriously. That's because TStringDynArray is not assignment compatible with TArray<string> or indeed any other type declared as array of string. It pays dividends to change your code base to use TArray<T> throughout.
Just in case anyone is interested in the rest of this helper class, here it is:
type
TArray = class(Generics.Collections.TArray)
private
class function Comparison<T>(SortType: TSortType): TComparison<T>; static;
class function Comparer<T>(const Comparison: TComparison<T>): IComparer<T>; static;
public
class procedure Swap<T>(var Left, Right: T); static;
class procedure Reverse<T>(var Values: array of T); static;
class function Reversed<T>(const Values: array of T): TArray<T>; static;
class function Contains<T>(const Values: array of T; const Item: T; out ItemIndex: Integer): Boolean; overload; static;
class function Contains<T>(const Values: array of T; const Item: T): Boolean; overload; static;
class function IndexOf<T>(const Values: array of T; const Item: T): Integer; static;
class function Sorted<T>(var Values: array of T; SortType: TSortType; Index, Count: Integer): Boolean; overload; static;
class function Sorted<T>(var Values: array of T; SortType: TSortType): Boolean; overload; static;
class function Sorted<T>(var Values: array of T; const Comparison: TComparison<T>; Index, Count: Integer): Boolean; overload; static;
class function Sorted<T>(var Values: array of T; const Comparison: TComparison<T>): Boolean; overload; static;
class function Sorted<T>(GetValue: TFunc<Integer,T>; const Comparison: TComparison<T>; Index, Count: Integer): Boolean; overload; static;
class procedure Sort<T>(var Values: array of T; SortType: TSortType; Index, Count: Integer); overload; static;
class procedure Sort<T>(var Values: array of T; SortType: TSortType); overload; static;
class procedure Sort<T>(var Values: array of T; const Comparison: TComparison<T>; Index, Count: Integer); overload; static;
class procedure Sort<T>(var Values: array of T; const Comparison: TComparison<T>); overload; static;
class function Copy<T>(const Source: array of T; Index, Count: Integer): TArray<T>; overload; static;
class function Copy<T>(const Source: array of T): TArray<T>; overload; static;
class procedure Move<T>(const Source: array of T; var Dest: array of T; Index, Count: Integer); overload; static;
class procedure Move<T>(const Source: array of T; var Dest: array of T); overload; static;
class function Concatenated<T>(const Source1, Source2: array of T): TArray<T>; overload; static;
class function Concatenated<T>(const Source: array of TArray<T>): TArray<T>; overload; static;
class procedure Initialise<T>(var Values: array of T; const Value: T); static;
class procedure Zeroise<T>(var Values: array of T); static;
class function GetHashCode<T>(const Values: array of T): Integer; overload; static;
class function GetHashCode<T>(Values: Pointer; Count: Integer): Integer; overload; static;
end;
class function TArray.Comparison<T>(SortType: TSortType): TComparison<T>;
var
DefaultComparer: IComparer<T>;
begin
DefaultComparer := TComparer<T>.Default;
Result :=
function(const Left, Right: T): Integer
begin
case SortType of
stIncreasing:
Result := DefaultComparer.Compare(Left, Right);
stDecreasing:
Result := -DefaultComparer.Compare(Left, Right);
else
RaiseAssertionFailed(Result);
end;
end;
end;
class function TArray.Comparer<T>(const Comparison: TComparison<T>): IComparer<T>;
begin
Result := TComparer<T>.Construct(Comparison);
end;
class procedure TArray.Swap<T>(var Left, Right: T);
var
temp: T;
begin
temp := Left;
Left := Right;
Right := temp;
end;
class procedure TArray.Reverse<T>(var Values: array of T);
var
bottom, top: Integer;
begin
bottom := 0;
top := high(Values);
while top>bottom do begin
Swap<T>(Values[bottom], Values[top]);
inc(bottom);
dec(top);
end;
end;
class function TArray.Reversed<T>(const Values: array of T): TArray<T>;
var
i, j, Count: Integer;
begin
Count := Length(Values);
SetLength(Result, Count);
j := Count-1;
for i := 0 to Count-1 do begin
Result[i] := Values[j];
dec(j);
end;
end;
class function TArray.Contains<T>(const Values: array of T; const Item: T; out ItemIndex: Integer): Boolean;
var
DefaultComparer: IEqualityComparer<T>;
Index: Integer;
begin
DefaultComparer := TEqualityComparer<T>.Default;
for Index := 0 to high(Values) do begin
if DefaultComparer.Equals(Values[Index], Item) then begin
ItemIndex := Index;
Result := True;
exit;
end;
end;
ItemIndex := -1;
Result := False;
end;
class function TArray.Contains<T>(const Values: array of T; const Item: T): Boolean;
var
ItemIndex: Integer;
begin
Result := Contains<T>(Values, Item, ItemIndex);
end;
class function TArray.IndexOf<T>(const Values: array of T; const Item: T): Integer;
begin
Contains<T>(Values, Item, Result);
end;
class function TArray.Sorted<T>(var Values: array of T; SortType: TSortType; Index, Count: Integer): Boolean;
begin
Result := Sorted<T>(Values, Comparison<T>(SortType), Index, Count);
end;
class function TArray.Sorted<T>(var Values: array of T; SortType: TSortType): Boolean;
begin
Result := Sorted<T>(Values, Comparison<T>(SortType));
end;
class function TArray.Sorted<T>(var Values: array of T; const Comparison: TComparison<T>; Index, Count: Integer): Boolean;
var
i: Integer;
begin
for i := Index+1 to Index+Count-1 do begin
if Comparison(Values[i-1], Values[i])>0 then begin
Result := False;
exit;
end;
end;
Result := True;
end;
class function TArray.Sorted<T>(var Values: array of T; const Comparison: TComparison<T>): Boolean;
begin
Result := Sorted<T>(Values, Comparison, 0, Length(Values));
end;
class function TArray.Sorted<T>(GetValue: TFunc<Integer, T>; const Comparison: TComparison<T>; Index, Count: Integer): Boolean;
var
i: Integer;
begin
for i := Index+1 to Index+Count-1 do begin
if Comparison(GetValue(i-1), GetValue(i))>0 then begin
Result := False;
exit;
end;
end;
Result := True;
end;
class procedure TArray.Sort<T>(var Values: array of T; SortType: TSortType; Index, Count: Integer);
begin
Sort<T>(Values, Comparison<T>(SortType), Index, Count);
end;
class procedure TArray.Sort<T>(var Values: array of T; SortType: TSortType);
begin
Sort<T>(Values, SortType, 0, Length(Values));
end;
class procedure TArray.Sort<T>(var Values: array of T; const Comparison: TComparison<T>; Index, Count: Integer);
begin
if not Sorted<T>(Values, Comparison, Index, Count) then begin
Sort<T>(Values, Comparer<T>(Comparison), Index, Count);
end;
end;
class procedure TArray.Sort<T>(var Values: array of T; const Comparison: TComparison<T>);
begin
Sort<T>(Values, Comparison, 0, Length(Values));
end;
class function TArray.Copy<T>(const Source: array of T; Index, Count: Integer): TArray<T>;
var
i: Integer;
begin
SetLength(Result, Count);
for i := 0 to high(Result) do begin
Result[i] := Source[i+Index];
end;
end;
class function TArray.Copy<T>(const Source: array of T): TArray<T>;
var
i: Integer;
begin
SetLength(Result, Length(Source));
for i := 0 to high(Result) do begin
Result[i] := Source[i];
end;
end;
class procedure TArray.Move<T>(const Source: array of T; var Dest: array of T; Index, Count: Integer);
var
i: Integer;
begin
for i := 0 to Count-1 do begin
Dest[i] := Source[i+Index];
end;
end;
class procedure TArray.Move<T>(const Source: array of T; var Dest: array of T);
var
i: Integer;
begin
for i := 0 to high(Source) do begin
Dest[i] := Source[i];
end;
end;
class function TArray.Concatenated<T>(const Source1, Source2: array of T): TArray<T>;
var
i, Index: Integer;
begin
SetLength(Result, Length(Source1)+Length(Source2));
Index := 0;
for i := low(Source1) to high(Source1) do begin
Result[Index] := Source1[i];
inc(Index);
end;
for i := low(Source2) to high(Source2) do begin
Result[Index] := Source2[i];
inc(Index);
end;
end;
class function TArray.Concatenated<T>(const Source: array of TArray<T>): TArray<T>;
var
i, j, Index, Count: Integer;
begin
Count := 0;
for i := 0 to high(Source) do begin
inc(Count, Length(Source[i]));
end;
SetLength(Result, Count);
Index := 0;
for i := 0 to high(Source) do begin
for j := 0 to high(Source[i]) do begin
Result[Index] := Source[i][j];
inc(Index);
end;
end;
end;
class procedure TArray.Initialise<T>(var Values: array of T; const Value: T);
var
i: Integer;
begin
for i := 0 to high(Values) do begin
Values[i] := Value;
end;
end;
class procedure TArray.Zeroise<T>(var Values: array of T);
begin
Initialise<T>(Values, Default(T));
end;
{$IFOPT Q+}
{$DEFINE OverflowChecksEnabled}
{$Q-}
{$ENDIF}
class function TArray.GetHashCode<T>(const Values: array of T): Integer;
// see http://stackoverflow.com/questions/1646807 and http://stackoverflow.com/questions/11294686
var
Value: T;
EqualityComparer: IEqualityComparer<T>;
begin
EqualityComparer := TEqualityComparer<T>.Default;
Result := 17;
for Value in Values do begin
Result := Result*37 + EqualityComparer.GetHashCode(Value);
end;
end;
class function TArray.GetHashCode<T>(Values: Pointer; Count: Integer): Integer;
// see http://stackoverflow.com/questions/1646807 and http://stackoverflow.com/questions/11294686
var
Value: ^T;
EqualityComparer: IEqualityComparer<T>;
begin
EqualityComparer := TEqualityComparer<T>.Default;
Result := 17;
Value := Values;
while Count>0 do begin
Result := Result*37 + EqualityComparer.GetHashCode(Value^);
inc(Value);
dec(Count);
end;
end;
{$IFDEF OverflowChecksEnabled}
{$Q+}
{$ENDIF}
The problem with
function MyFunc: TStringDynArray;
begin
Result := ['Data1', 'Data2'];
end;
is that ['Data1', 'Data2'] is interpreted as a set.
I sometimes use the following convenience function (but usually not in performance-critical sections):
function MakeStringArray(const Strings: array of string): TStringDynArray;
var
i: Integer;
begin
SetLength(Result, Length(Strings));
for i := Low(Strings) to High(Strings) do
Result[i] := Strings[i];
end {MakeStringArray};
Using that, you could rewrite your first example as follows:
function MyFunc: TStringDynArray;
....
function MyFunc: TStringDynArray;
begin
Result := MakeStringArray(['Data1', 'Data2']);
end;
But since you're using XE3, you're better off using TStringDynArray.Create, like Andreas Rejbrand suggests.