Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2022-12

Open array in object constructor gives error

15 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Hairy Pixels

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.

Regards,
Ryan Joseph

Reply by email

Mattias Gaertner
to Hairy Pixels
Show quoted text 19 lines
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...

Not supported. Has nothing to do with the parameter list.

end.

Mattias

Reply by email

Hairy Pixels
to Mattias Gaertner
On Dec 31, 2022, at 3:43 PM, Mattias Gaertner via fpc-pascal wrote: Not supported. Has nothing to do with the parameter list.

That’s disappointing. :( More reason we need an “advanced objects” mode switch.

Regards,
Ryan Joseph

Reply by email

Sven Barth
to Hairy Pixels: Why is using the open array parameter illegal using the Object type?…
Hairy Pixels wrote 20 lines
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

Reply by email

Hairy Pixels
to Sven Barth
On Jan 3, 2023, at 1:36 PM, Sven Barth wrote: 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:

But why do you need to use New if you just want a stack allocated object? The constructor is called without New I thought?

Regards,
Ryan Joseph

Reply by email

Sven Barth
to Hairy Pixels

Hairy Pixels schrieb am Di., 3. Jan. 2023, 08:04:

Show quoted text 12 lines
> On Jan 3, 2023, at 1:36 PM, Sven Barth wrote: > > 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: But why do you need to use New if you just want a stack allocated object? The constructor is called without New I thought?

But then you don't use the type name either. You simply do "o.Create(...)".

Regards,
Sven

Reply by email

Hairy Pixels
to Sven Barth
On Jan 3, 2023, at 3:12 PM, Sven Barth wrote: But then you don't use the type name either. You simply do "o.Create(...)".

Ohhhh I get it now. I didn’t realize you couldn’t create on the type. That breaks my entire design then. It would be great to have a modern object that behaved more like classes but on the stack (like C++ does).

Regards,
Ryan Joseph

Reply by email

Marc Weustink
to Hairy Pixels
Hairy Pixels wrote 8 lines
> On Jan 3, 2023, at 3:12 PM, Sven Barth wrote: > > But then you don't use the type name either. You simply do "o.Create(...)". > Ohhhh I get it now. I didn’t realize you couldn’t create on the type. That breaks my entire design then. It would be great to have a modern object that behaved more like classes but on the stack (like C++ does).

On what instance/variable/piece of memory would your code "TMyObject.Create(['1', '2', '3'])" then operate ?

Marc

Reply by email

Sven Barth
to Hairy Pixels: Ohhhh I get it now. I didn’t realize you couldn’t create on the type. That breaks my…

Hairy Pixels schrieb am Di., 3. Jan. 2023, 14:07:

Show quoted text 13 lines
> On Jan 3, 2023, at 3:12 PM, Sven Barth wrote: > > But then you don't use the type name either. You simply do "o.Create(...)". > Ohhhh I get it now. I didn’t realize you couldn’t create on the type. That breaks my entire design then. It would be great to have a modern object that behaved more like classes but on the stack (like C++ does).

In C++ you can't use polymorphism either if the class is allocated on the stack, so TP-style objects and C++-classes behave the same here.

Regards,
Sven

Reply by email

Sven Barth
to Hairy Pixels

Hairy Pixels via fpc-pascal schrieb am
Mi., 4. Jan. 2023, 02:40:

Show quoted text 11 lines
> On Jan 3, 2023, at 8:39 PM, Marc Weustink via fpc-pascal < > wrote: > > On what instance/variable/piece of memory would your code "TMyObject.Create(['1', '2', '3'])" then operate ? There’s a global stack which is opened and they are stored there during construction.

For something like that you are better of to use Object Pascal classes and override their NewInstance and FreeInstance methods.

Regards,
Sven

Reply by email

Hairy Pixels
to Sven Barth
On Jan 4, 2023, at 2:01 PM, Sven Barth wrote: For something like that you are better of to use Object Pascal classes and override their NewInstance and FreeInstance methods.

What do you mean exactly? You still need to allocate some memory for them right so what do you gain?

Regards,
Ryan Joseph

Reply by email

Sven Barth
to Hairy Pixels

Hairy Pixels schrieb am Mi., 4. Jan. 2023, 13:06:

Show quoted text 12 lines
> On Jan 4, 2023, at 2:01 PM, Sven Barth wrote: > > For something like that you are better of to use Object Pascal classes and override their NewInstance and FreeInstance methods. > What do you mean exactly? You still need to allocate some memory for them right so what do you gain?

You said in the other mail "there's a global stack", so just return memory from that stack.

Regards,
Sven

Reply by email

Hairy Pixels
to Sven Barth
On Jan 4, 2023, at 8:23 PM, Sven Barth wrote: You said in the other mail "there's a global stack", so just return memory from that stack.

Well the stack is just a list that holds pointers to the classes now but I guess you can modify that so it turns a pointer to an address in a block of memory? I’ve not seen this kind of data type (except for an actual memory manager) so I would need to create it.

Regards,
Ryan Joseph

Reply by email

Sven Barth
to Hairy Pixels

Hairy Pixels schrieb am Mi., 4. Jan. 2023, 14:45:

Show quoted text 13 lines
> On Jan 4, 2023, at 8:23 PM, Sven Barth wrote: > > You said in the other mail "there's a global stack", so just return memory from that stack. Well the stack is just a list that holds pointers to the classes now but I guess you can modify that so it turns a pointer to an address in a block of memory? I’ve not seen this kind of data type (except for an actual memory manager) so I would need to create it.

If your stack only contains a lost of pointers then it's back to the original question from Marc: on what memory does your TMyObject.Create() operate? Where does the memory come from you put into your stack?

Regards,
Sven

Reply by email