Sign in Join

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

Mailing lists / fpc-devel / 2025-11

Impossible CONST generic constraints in forward declarations...?

4 messages · 1 person ·

Reply by email Subscribe to this list Your subscription

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

Martin Frb

According to
https://forum.lazarus.freepascal.org/index.php/topic,52157.msg383991.html#msg383991

1) Contstraint given in the interface declaration MUST be dropped in the implementation
 (though it does not explicitly mention methods in a class in the interface or implementation)

2) Constraints in a "forward" declaration in the implementation (or a program?) must be kept.

But I cannot figure out how to do it for "const" constraints

For FooProc3 neither keeping, nor dropping works. Both give errors.

program Project1; {$mode ObjFPC}
type
   generic TGeneric1<G1Param> = class end;
   TFoo = class
     generic procedure FooProc1<T1: specialize TGeneric1<byte>; T2:
TObject>(a: T1);
     generic procedure FooProc2<T1; T2: TObject>(a: T1);
     generic procedure FooProc3<T1; const T2: integer>(a: T1);
   end;
generic procedure TFoo.FooProc1<T1; T2>(a: T1); begin {} end;
generic procedure TFoo.FooProc2<T1; T2>(a: T1); begin {} end;
//generic procedure TFoo.FooProc3<T1; T2>(a: T1); begin {} end;  //
project1.lpr(16,24) Error: Function header doesn't match any method of
this class "FooProc3$2(T1);"
generic procedure TFoo.FooProc3<T1; const T2: integer>(a: T1); begin {}
end;   // project1.lpr(17,43) Error: Generic constraint not allowed here
begin
end.

Also, forward seems to have lost all checking.
It compiles, even if the forward has a different constraint.

program Project1;
{$mode ObjFPC}
   type
     TX1 = class end;
     TX2 = class end;
   generic procedure Proc4<T1; T2: TX1>(a: T1); forward;
   generic procedure Proc4<T1; T2:TX2>(a: T1); begin {} end;
begin
end.

Reply by email

Martin Frb
to Martin Frb
Martin Frb wrote 6 lines
According to https://forum.lazarus.freepascal.org/index.php/topic,52157.msg383991.html#msg383991 2) Constraints in a "forward" declaration in the implementation (or a program?) must be kept.

Actually, that does not seem to be true either...

The below only compiles when they are NOT kept (same if converted to mode delphi)

unit Unit1;{$mode ObjFPC}
interface
implementation
generic procedure Foo3<A: TObject;B>(p1: A); forward;
generic procedure Foo3<A; B>(p1: A); begin end;
end.

However,
in a program, they must be dropped.
(it will not compile, if the :TObject is kept)

program Project1;
generic procedure Foo3<A: TObject;B>(p1: A); forward;
generic procedure Foo3<A; B>(p1: A);
begin  end;

begin end.

--------------
More, it seems that many checks are performed only on the FIRST gen param. Changes in the rest of them seems to be ignored, at least sometimes...

Reply by email

Martin Frb
to Martin Frb
Martin Frb wrote 9 lines
On 25/11/2025 20:41, Martin Frb via fpc-devel wrote: > According to > https://forum.lazarus.freepascal.org/index.php/topic,52157.msg383991.html#msg383991 > > > 2) Constraints in a "forward" declaration in the implementation (or a > program?) must be kept. Actually, that does not seem to be true either...

Sorry, the evening seems to have been to long....

I misread the post.
- "const constraints" (value param) must be repeated on forward, and forward only.
- All others must always be stripped.

That leaves 2 other issues

1)
The example from the link, shouldn’t that be a ; instead of the ,
generic procedure Test<T,const N:Integer>(A: T); forward;
generic procedure Test<T,const N:Integer>(A: T); begin end;

(even if the const kind of clarifies that integer only applies to N, not to T...)

2)
from my 1st post
fpc 3.3.1 only

it seems it forgets some checks?
T2 changes to an incompatible constraint.

program Project1;
{$mode ObjFPC}
   type
     TX1 = class end;
     TX2 = class end;
   generic procedure Proc4<T1;   T2: TX1>(a: T1); forward;
   generic procedure Proc4<T1;   T2:TX2>(a: T1); begin {} end;
begin
end.

Reply by email

Martin Frb
to Martin Frb
Martin Frb wrote 18 lines
On 25/11/2025 22:02, Martin Frb via fpc-devel wrote: > On 25/11/2025 20:41, Martin Frb via fpc-devel wrote: >> According to >> https://forum.lazarus.freepascal.org/index.php/topic,52157.msg383991.html#msg383991 >> >> >> 2) Constraints in a "forward" declaration in the implementation (or >> a program?) must be kept. > > Actually, that does not seem to be true either... Sorry, the evening seems to have been to long.... I misread the post. - "const constraints" (value param) must be repeated on forward, and forward only. - All others must always be stripped.

Then again, below does not compile

program Project1;{$Mode objfpc}
generic procedure Foo<const A: integer;B: TObject;C>(p1: B); forward;
generic procedure Foo<const A: integer; B; C>(p1: B);
begin {} end;
begin
end.

Reply by email