5

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?

2 Answers 2

6

You could typecast the left hand side of the assignment:

TSomeTypeArray(anArray) := GetSomeTypeArray();
Sign up to request clarification or add additional context in comments.

1 Comment

Left hand side typecast <- this blows my mind so much
3

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.

1 Comment

Yeah, sorry. AFAIK that is the only way, and since no one else piped in with an alternative . . .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.