Why are two seemingly identical types of dynamic arrays considered incompatible with assignment?

Just a small question, I can’t find a specific answer, so I guessed that it might be faster to ask here.

The compiler rejects the code below with the following error:

incompatible types 'dynamic array' and 'array of string'

TMailInfo = record FileName, MailAdresse, MailBCC, MailCC, MailBetreff: string; MailText, Anhang: array of string; MailAcknowledge, MailTXT: Boolean end; class function TEMail.SendOutlookCOMMail(aFileName, aMailAdresse, aMailBCC, aMailCC, aMailBetreff: string; aMailText, aAnhang: array of string; const aMailAcknowledge, aMailTXT: Boolean): Boolean; var mailInfo: TMailInfo; begin ... mailInfo.MailBetreff := aMailBetreff; // these two lines cause the error mailInfo.MailText := aMailText; ... end; 

What am I doing wrong? Both are arrays of strings, so I don’t understand why one seems dynamic.

+6
source share
2 answers

You cannot easily designate MailText and Anhang because you cannot declare another object with a compatible type. This is because you used the dynamic inline array type in your write declaration. You really need to use a type that you can name. To illustrate this a little better, consider the following:

 X: array of Integer; Y: array of Integer; 

Now X and Y are of different types and X := Y does not compile.

Another problem is your open array parameter. An open array parameter is an assignment that is not compatible with anything. You can copy element by element, but you cannot assign an array at a time.

The best way out of this is to declare the field as follows:

 MailText, Anhang: TArray<string>; 

And change the parameters of the open array in the function in the same way as for this type.

Then you need to decide if you want to copy the link or array:

 MailText := aMailText; // copy reference, or MailText := Copy(aMailText); // copy array 
+9
source

You have two problems. First, as David says, two type declarations that look the same but are declared separately, make them different, assign incompatible types.

Oddly enough, this is not the case for typical types such as TArray<string> , so it makes sense to use this if you support the Delphi version.

But the second problem is that you confuse an open array parameter, such as aMailText , with a dynamic array of type mailInfo.MailText . aMailText , the parameter is not necessarily a dynamic array at all, it can be any type of array.

Take a look at the documentation: Open Arrays

Another explanation: Open array parameters and constant array

+5
source

Source: https://habr.com/ru/post/972435/


All Articles