Florian Koch
Florian Koch

Reputation: 1502

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.

Upvotes: 6

Views: 4054

Answers (2)

Rudy Velthuis
Rudy Velthuis

Reputation: 28806

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

Weirdly enough, this is not so for generic types, like TArray<string>, so it makes sense to use that, if you version of Delphi supports it.

But the second problem is that you are confusing an open array parameter like aMailText with a dynamic array like mailInfo.MailText. aMailText, the parameter, is not necessarily a dynamic array at all, it can be any kind of array.

Take a look at the documentation: Open Arrays

Another explanation: Open array parameters and array of const

Upvotes: 5

David Heffernan
David Heffernan

Reputation: 612954

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

Upvotes: 10

Related Questions