Sign in Join

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

Mailing lists / fpc-pascal / 2025-11

Override constructor in class helper?

4 messages · 4 people ·

Reply by email Subscribe to this list Your subscription

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

Thomas Kurz

Hello,

what I'm trying to do is something like this:

TStringStreamHelper = class helper for TStringStream
  constructor Create;
  constructor Create (const AString: AnsiString);
end;
constructor TStringStreamHelper.create;
begin
  inherited Create ('', TEncoding.UTF8);
end;
constructor TStringStreamHelper.create (const AString: AnsiString);
begin
  inherited Create (AString, TEncoding.UTF8);
end;

Both ChatGPT and Gemini agree that this won't work because FPC resolves Create in this order:
1. Look at the real class (TStringList) for a matching constructor.
2. Only if no such method exists, check helpers in scope for a matching name.

However, if I try, the helper constructor *is* invoked.

Because I'm on FPC 3.3.1 trunk, I wonder whether this is indended behavior and I can rely on it in the future, or whether this is a bug and will break in the final 3.4.0 release.

It'd be nice if someone could clarify this.

Thank you
Thomas

Mattias Gaertner
to Thomas Kurz: what I'm trying to do is something like this: TStringStreamHelper = class helper for…
Thomas Kurz wrote 22 lines
Hello, what I'm trying to do is something like this: TStringStreamHelper = class helper for TStringStream constructor Create; constructor Create (const AString: AnsiString); end; constructor TStringStreamHelper.create; begin inherited Create ('', TEncoding.UTF8); end; constructor TStringStreamHelper.create (const AString: AnsiString); begin inherited Create (AString, TEncoding.UTF8); end; Both ChatGPT and Gemini agree that this won't work because FPC resolves Create in this order: 1. Look at the real class (TStringList) for a matching constructor. 2. Only if no such method exists, check helpers in scope for a matching name.

I hope you did not pay them for that answer.

However, if I try, the helper constructor *is* invoked. Because I'm on FPC 3.3.1 trunk, I wonder whether this is indended behavior and I can rely on it in the future, or whether this is a bug and will break in the final 3.4.0 release.

Intended.

Mattias

Sven Barth
to Thomas Kurz: what I'm trying to do is something like this: TStringStreamHelper = class helper for…

Thomas Kurz via fpc-pascal schrieb am
Do., 27. Nov. 2025, 11:33:

Show quoted text 6 lines
Both ChatGPT and Gemini agree that this won't work because FPC resolves Create in this order: 1. Look at the real class (TStringList) for a matching constructor. 2. Only if no such method exists, check helpers in scope for a matching name.

What about asking real people from the beginning instead of stochastic parrots?
Declaring constructors in helpers is indeed supported.

Regards,
Sven