Sign in Join

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

Mailing lists / fpc-pascal / 2015-11

Generic with inheritance - fpc 3.0

3 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

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

Torsten Bonde Christiansen

Hi.

The docs for 3.0 does not directly mention generics and inheriting from another generic class, but I tried out with the code below. It compiled, ran and worked fine but since the docs do not explicit mention it as a posibility I am a little reluctant to go forward using this strategy if is not meant to work...

(ps. i know this is incomplete code, but it should illustrate my point)type

type
   { TMyType }
   generic TMyType<T> = class
   private
     Item: T;
   public
     constructor Create; virtual;
   end;
   { TMyDerivedType }
   generic TMyDerivedType<T> = class(specialize TMyType<T>)
   private
     Count: Integer;
   public
     constructor Create; override;
   end;
   TMyIntClass = specialize TMyDerivedType<Integer>;

[...snipped both Create implementations...]

var
   IC: TMyIntClass;
begin
   M := TMyIntClass.Create();
   M.Item := 20;
   M.Count := 2;
   M.Free;
end;
Sven Barth
to Torsten Bonde Christiansen
"Torsten Bonde Christiansen" wrote
Hi. The docs for 3.0 does not directly mention generics and inheriting from

another generic class, but I tried out with the code below.

It compiled, ran and worked fine but since the docs do not explicit

mention it as a posibility I am a little reluctant to go forward using this strategy if is not meant to work...

Yes, that's supposed to work :)

Regards,
Sven

Michael Van Canneyt
to Sven Barth
Sven Barth wrote 11 lines
Am 30.11.2015 15:55 schrieb "Torsten Bonde Christiansen" : > > Hi. > > The docs for 3.0 does not directly mention generics and inheriting from another generic class, but I tried out with the code below. > It compiled, ran and worked fine but since the docs do not explicit mention it as a posibility I am a little reluctant to go forward using this strategy if is not meant to work... Yes, that's supposed to work :)

I will add this to the documentation.

Michael.