Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

type casting to a dynamic array

Given the following:

Type
  TSomeTypeArray = array of SomeType;

var
  anArray: array of SomeType;

function GetSomeTypeArray: TSomeTypeArray; 

I want to write anArray = GetSomeTypeArray(); but the compiler does not like it. Without changing the type of anArray or the return type of GetSomeTypeArrayhow can I typecast TSomeTypeArray to array of SomeType?

like image 835
Asher Avatar asked Jan 21 '23 00:01

Asher


2 Answers

You could typecast the left hand side of the assignment:

TSomeTypeArray(anArray) := GetSomeTypeArray();
like image 184
Dan Bartlett Avatar answered Jan 26 '23 23:01

Dan Bartlett


You can't. You need to declare anArray as of type TSomeTypeArray, then it should work.

Alternatively, you could store the result into another array of type TSomeTypeArray then call SetLength on anArray to the length of the returned array. And finally loop through the two arrays setting the elements of anArray to the elements of the returned array.

like image 26
Jim McKeeth Avatar answered Jan 27 '23 00:01

Jim McKeeth