Sign in Join

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

Mailing lists / fpc-devel / 2026-02

generic and type compatibility / interface vs class ...

8 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

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

Martin Frb
ok, so if I have
    procedure bar(f: IntfFoo);
I can do
    bar(SomeClassInstanceThatImplementsFoo);

makes sense, the interface can be gotten at the time of calling.

Now if I have

  generic MyGen<    P: IntfFoo >

then I can also do
  specialize MyGen<SomeClassInstanceThatImplementsFoo>

is that indented?

Because now if I have in MyGen

    FMyField: P;

and I try to assign an interface to it... Then that can't work.

Of course there are cases where it is the other way round, and may be useful.

------------------
Just trying to find out if that is a feature, or an issue that will eventually be changed?

Reply by email

Michael Van Canneyt
to Martin Frb
Martin Frb wrote 16 lines
ok, so if I have    procedure bar(f: IntfFoo); I can do    bar(SomeClassInstanceThatImplementsFoo); makes sense, the interface can be gotten at the time of calling. Now if I have   generic MyGen<    P: IntfFoo > then I can also do   specialize MyGen<SomeClassInstanceThatImplementsFoo> is that indented?

I don't think so. Sounds like a bug ?

Michael.

Reply by email

Hairy Pixels
to Michael Van Canneyt

SomeClassInstanceThatImplementsFoo is a variable? variables should never be never allowed within , which only allow types and constants.

On Feb 3, 2026 at 9:16:28 PM, Michael Van Canneyt via fpc-devel <

Show quoted text 40 lines
wrote: On Tue, 3 Feb 2026, Martin Frb via fpc-devel wrote: ok, so if I have procedure bar(f: IntfFoo); I can do bar(SomeClassInstanceThatImplementsFoo); makes sense, the interface can be gotten at the time of calling. Now if I have generic MyGen< P: IntfFoo > then I can also do specialize MyGen<SomeClassInstanceThatImplementsFoo> is that indented? I don't think so. Sounds like a bug ? Michael. _______________________________________________ fpc-devel maillist - https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel

Reply by email

Martin Frb
to Hairy Pixels
Hairy Pixels wrote
SomeClassInstanceThatImplementsFoo is a variable? variables should never be never allowed within , which only allow types and constants.

Ah, sorry, copy and paste.

Its a var for the function call.

The specrialize should have read
     specialize MyGen<Some__Class__ThatImplementsFoo>

So I have a constrained that wants an interface, but I can specialize with a class.

Reply by email

Michael Van Canneyt
to Martin Frb
Martin Frb wrote 12 lines
On 03/02/2026 15:37, Hairy Pixels via fpc-devel wrote: > SomeClassInstanceThatImplementsFoo is a variable? variables should > never be never allowed within , which only allow types and constants. Ah, sorry, copy and paste. Its a var for the function call. The specrialize should have read      specialize MyGen<Some__Class__ThatImplementsFoo> So I have a constrained that wants an interface, but I can specialize with a class.

That should not work when you're defining a type.

Michael.

Reply by email

Hairy Pixels
to Martin Frb

On Feb 3, 2026 at 10:42:05 PM, Martin Frb via fpc-devel <

program Project1;
{$Mode objfpc}
{$Interfaces CORBA}
type
  IFoo = interface end;
  TBar = class(TObject, IFoo) end;
  generic MyGen<A: IFoo> = class end;
  TSome = specialize MyGen<TBar>;
begin
end.

ok so I can see the whole thing now and this should compile. TBar implements IFoo and "A" must conform to IFoo so TBar is compatible with "A".

Reply by email

Martin Frb
to Hairy Pixels
Hairy Pixels wrote 22 lines
On Feb 3, 2026 at 10:42:05 PM, Martin Frb via fpc-devel wrote: > https://gitlab.com/freepascal.org/fpc/source/-/issues/41604 program Project1; {$Mode objfpc} {$Interfaces CORBA} type IFoo = interface end; TBar = class(TObject, IFoo) end; generic MyGen<A: IFoo> = class end; TSome = specialize MyGen<TBar>; begin end. ok so I can see the whole thing now and this should compile. TBar implements IFoo and "A" must conform to IFoo so TBar is compatible with "A".

Not really.

Below stops compiling, when you uncomment the specialize.

IFoo has a method Test.

TBar does not have that. Only TBar as IFoo would have it. But it isn't cast to IFoo.

program Project1;
{$Mode objfpc}
{$Interfaces CORBA}
type
   IFoo = interface
     procedure Test;
   end;
   TBar = class(TObject, IFoo)
     procedure BarTest; virtual; abstract;
     procedure IFoo.Test = BarTest;
   end;
   generic MyGen<A: IFoo> = class
     procedure p1(x: A);
   end;
   //TSome = specialize MyGen<TBar>;
procedure MyGen.p1(x: A);
begin
   x.Test
end;
begin
end.

Reply by email