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

…衆ロ難τιáo~ 提交于 2019-12-23 09:43:07

问题


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.


回答1:


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



回答2:


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



来源:https://stackoverflow.com/questions/24818618/why-are-two-seemingly-identical-dynamic-array-types-deemed-not-assignment-compat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!