Sign in Join

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

Mailing lists / fpc-pascal / 2011-11

Inheriting from generics specialized classes

6 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

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

Juha Manninen

... Sorry, forgot subject first....

I try to learn to use generics properly.
First, this is a class definition without generics:

  TMyDerived = class(TObjectList)
  private
    MyInt: integer;
  public
    constructor Create(FreeObjects: Boolean=True);
  end;

It works. Now I try to derive a class with generics:

  TMyGen = specialize TFPGObjectList<TControl>;
  TMyDerived = class(TMyGen)
  private
    MyInt: integer;
  public
    constructor Create(FreeObjects: Boolean=True);
  end;
It says:
 unit1.pas(40,24) Error: Duplicate identifier "FreeObjects"

Why? If I change "FreeObjects" to "aFreeObjects" then it works. Again why?

Is it even ok to derive classes from specialized classes like that?

Reply by email

Sven Barth
to Juha Manninen
Juha Manninen wrote 14 lines
Now, if I do: myV := TMyDerived.Create(False); if myV is TFPGObjectList then ... it gives: unit1.pas(67,10) Error: Class or Object types "TMyDerived" and "TFPGObjectList" are not related In my opinion they are related. Maybe generics are good only for situations where you don't need to derive classes or test their inheritance.

They are not related. You should not think about generics as full classes. They are merely "templates". If you do a "specialize TFPGObjectList<SomeType>" the compiler will use the recorded declaration and implementation of the "TFPGObjectList", replace all occurences of the type "T" by "SomeType" and will parse the specialization as if you had written it like that yourself.

You'd need to check against your specialized "TMyGen" instead of "TFPGObjectList" then it would work.

In mode Delphi and my generic branch (I hope ^^) you are able to do it like this as well:

myV := TMyDerived.Create(False);
if myV is TFPGObjectList<TMyGen> then
   ...

Under the condition that "TMyDerived" is declared like in your previous mail.

Regards,
Sven

Reply by email

Juha Manninen
to Sven Barth

2011/11/24 Sven Barth

Thanks for the explanations.

In mode Delphi and my generic branch (I hope ^^) you are able to do it like

Show quoted text 9 lines
this as well: myV := TMyDerived.Create(False); if myV is TFPGObjectList<TMyGen> then ... Under the condition that "TMyDerived" is declared like in your previous mail.
I think the syntax you gave should be
 if myV is TFPGObjectList<TControl> then

I will continue learning where to use generics and where not to use them.

Regards,
Juha

Reply by email

Sven Barth
to Juha Manninen
Juha Manninen wrote 19 lines
2011/11/24 Sven Barth < > Thanks for the explanations. In mode Delphi and my generic branch (I hope ^^) you are able to do it like this as well: myV := TMyDerived.Create(False); if myV is TFPGObjectList<TMyGen> then ... Under the condition that "TMyDerived" is declared like in your previous mail. I think the syntax you gave should be if myV is TFPGObjectList<TControl> then

Yes, of course. Thank you for the correction.

I will continue learning where to usegenerics and where not to use them.

Have fun and feel free to ask further questions :)

Regards,
Sven

Reply by email