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

2023-05-10 Thread Michael Van Canneyt via fpc-pascal




On Wed, 10 May 2023, Thomas Kurz via fpc-pascal wrote:


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.


I would expect the type helper to remain functional. The types are the same
for all purposes except they have a different RTTI entry.

But the exact behaviour of the compiler in this case is something that Sven
Barth will need to explain...

Michael.
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


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

2023-05-10 Thread Thomas Kurz via fpc-pascal
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 
To: Thomas Kurz via fpc-pascal 
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;

> {$MODE OBJFPC}
> {$MODESWITCH ADVANCEDRECORDS}
> {$MODESWITCH TYPEHELPERS}

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

Michael.
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal