[fpc-pascal] Using "array of xxx" in function call, Delphi compatibility
Sven Barth
pascaldragon at googlemail.com
Sat Aug 13 18:17:36 CEST 2011
On 13.08.2011 13:25, Jonas Maebe wrote:
>
> On 13 Aug 2011, at 12:28, Juha Manninen wrote:
>
>> TMainMenu.Items is of type TMenuItem. TMenuItem has only one version of
>> "Add" method and it takes another TMenuItem as parameter (checked from VCL
>> source).
>> Still, to my surprise, the code works in Delphi.
>>
>> FPC says: Error: Incompatible type for arg no. 1: Got "Dynamic Array Of
>> TMenuItem", expected "TMenuItem"
>> which is what I expected also from Delphi.
>
> It is always best to isolate such things in a self-contained example that does not depend on external code. Does this compile with your Delphi version?
>
> ***
> type
> tc = class
> end;
>
> procedure add(c: tc);
> begin
> end;
>
> var
> a: array of tc;
> begin
> add(a);
> end.
> ***
>
> If not, the issue is something else. It seems more likely to me that there is somewhere another "add" method that Delphi picks up but FPC doesn't. E.g., there is at least also "TComponent.Add(item : IUnknown) : Integer;". While that one won't accept a dynamic array, maybe Delphi 2009 has more add methods in TComponent declared with "overload" (although normally searching for overloads should stop as soon as a method is found in a class without "overload", so in principle the search should stop when tmenuitem.add is encountered).
>
> If the above does compile, what does it actually do? Pass the first element of the array?
Related question, but the other way round. The following code does
compile in FPC (didn't test Delphi). Is that by design?
=== source begin ===
procedure Test(a: array of String);
var
s: String;
begin
for s in a do
Writeln(s);
end;
procedure Test2(a: array of Integer);
var
i: Integer;
begin
for i in a do
Writeln(i);
end;
var
s: String;
i: Integer;
begin
s := 'Hello World';
Test(s);
i := 42;
Test2(i);
end.
=== source end ===
Calling "Test" with a constant string ("Test('Hello World');") or
"Test2" with a constant integer ("Test2(42);") does not compile though...
Regards,
Sven
More information about the fpc-pascal
mailing list