[fpc-pascal] Using "array of xxx" in function call, Delphi compatibility

Ludo Brands ludo.brands at free.fr
Sat Aug 13 13:42:47 CEST 2011


> > 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?
> 

Delphi has a procedure TMenuItem.Add(const AItems: array of TMenuItem);
overload;  which isn't implemented in Lazarus. It existed already in
Delphi6. So, not a compiler issue.

Ludo 




More information about the fpc-pascal mailing list