[fpc-pascal] Open array in object constructor gives error

Sven Barth pascaldragon at googlemail.com
Tue Jan 3 07:36:05 CET 2023


Am 31.12.2022 um 04:35 schrieb Hairy Pixels via fpc-pascal:
> Why is using the open array parameter illegal using the Object type?
>
> ========================
>
> {$mode objfpc}
>
> program test;
>
> type
>    TMyObject = object
>      constructor Create(text: array of String);
>    end;
>
> constructor TMyObject.Create(text: array of String);
> begin
> end;
>
> begin
>    TMyObject.Create(['1', '2', '3']); // error: Illegal expression
> end.

Objects are not classes, they don't know the Object Pascal style syntax 
for creating them, instead you need to use the syntax for objects from 
TP times:

=== code begin ===

program tobj;

type
   TMyObject = object
     constructor Create(text: array of String);
     destructor Destroy;
   end;
   PMyObject = ^TMyObject;

constructor TMyObject.Create(text: array of String);
begin
   Writeln(Length(text));
end;

destructor TMyObject.Destroy;
begin

end;

var
   o: PMyObject;
begin
   New(o, Create(['1', '2', '3']));
   Dispose(o, Destroy);
end.

=== code end ===

Regards,
Sven


More information about the fpc-pascal mailing list