Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are two seemingly identical dynamic array types deemed not assignment compatible?

Just a little question, I'm not finding a specific answer so i guessed 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 string, so I don't get why one seems to be dynamic.

like image 584
Florian Koch Avatar asked Jul 18 '14 06:07

Florian Koch


1 Answers

You cannot readily assign to MailText and Anhang because you cannot declare another object with a compatible type. That's because you used a dynamic array type inline in your record declaration. You really need to use a type that can be named. To illustrate a bit better, consider this:

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

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

The other problem is your open array parameter. An open array parameter is assignment compatible with nothing. You can copy element by element, but you cannot assign the array in one go.

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

MailText,
Anhang: TArray<string>;

And change the open array parameters in the function to also be of that type.

Then you need to decide whether you want to copy the reference or the array:

MailText := aMailText; // copy reference, or
MailText := Copy(aMailText); // copy array
like image 147
David Heffernan Avatar answered Nov 15 '22 06:11

David Heffernan