[fpc-pascal] Record Constructors which differ in parameter lists

Thomas Kurz fpc.2021 at t-net.ruhr
Wed May 10 15:56:32 CEST 2023

Dear Michael,

thank you for the explanation. I understand that the helper overwrites the original constructor. But...

I have defined "type TSomething = type TVec3f" (note the second "type"). So from my perspective, I'd assume that the TSomethingHelper doesn't apply to TVec3f at all because TSomething is defined as a new type, not only an alias for TVec3f.

----- Original Message ----- 
From: Michael Van Canneyt via fpc-pascal <fpc-pascal at lists.freepascal.org>
To: Thomas Kurz via fpc-pascal <fpc-pascal at lists.freepascal.org>
Sent: Tuesday, May 9, 2023, 23:55:47
Subject: [fpc-pascal] Record Constructors which differ in parameter lists

On Tue, 9 May 2023, Thomas Kurz via fpc-pascal wrote:

> Hello,

> let's take the following example:

> program Project1;


> type TVec3f = record
>  x, y, z: Double;
>  constructor Create (a1, a2, a3: Double);
> end;

> type TSomething = type TVec3f;

> type TSomethingHelper = type helper for TSomething
>  constructor Create (a1, a2: Double);
> end;

> constructor TVec3f.Create (a1, a2, a3: Double);
> begin
>  Self.x := a1;
>  Self.y := a2;
>  Self.z := a3;
> end;

> constructor TSomethingHelper.Create (a1, a2: Double);
> begin
>  Self.x := a1;
>  Self.y := a2;
>  Self.z := 1 - a1 - a2;
> end;

> var f: TVec3f;

> begin
>  f := TVec3f.Create (0.0, 0.0, 0.0);     // <-- error here
> end.

> I get the error:
> project1.lpr(35,37) Error: Wrong number of parameters specified for call to "Create"
> project1.lpr(25,30) Error: Found declaration: constructor Create(Double;Double);

> So, obviously, FPC tries to apply the helper of TSomething (which should be decoupled from TVec3f because of the "type TVec3f" declaration) to TVec3f which hasn't defined any constructor with 2 parameters.

> Is this intended behavior or should I report it as a bug?

This is intended behaviour since declarations in helpers always take
precedence over the methods in the class/record.

Try to add overload; to the constructor in the helper. This is a hint to the
compiler that it should look for other methods.

fpc-pascal maillist  -  fpc-pascal at lists.freepascal.org

More information about the fpc-pascal mailing list