Sign in Join

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

Mailing lists / fpc-pascal / 2011-11

(no subject)

2 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

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?

Juha

Sven Barth
to Juha Manninen
Juha Manninen wrote
It says: unit1.pas(40,24) Error: Duplicate identifier "FreeObjects" Why? If I change "FreeObjects" to "aFreeObjects" then it works. Again why?

You simply picked a bad example, because "TFPGObjectList" contains a property called "FreeObjects", thus mode ObjFPC will not allow you to declare a similar identifier again in this class.

Though it's inconsistent (and in my opinion a bug) that the duplicated declaration inside "TFPGObjectList" itself is not detected...

Note: if the property in "TFPGObjectList" would be moved before the constructor it should be detected as duplicate.

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

Of course it is. In Delphi (and my generics branch) even the following is allowed:

{$mode delphi}
type
   TSomeDerivedList<T> = class(TFPGObjectList<T>)
     // ...
   end;

Regards,
Sven