Overriding generic methods

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Overriding generic methods

Ryan Joseph
Is overriding methods from specialized generic classes supported in any way? I tried the following below but it doesn’t seem to work.

type
  generic TList<T> = class (TObject)
                procedure Add (value: T); virtual;
        end;


type
        TObjectListAbstract = specialize TList<TObject>;
  TObjectList = class (TObjectListAbstract)
                procedure Add (value: T); override;
        end;




Regards,
        Ryan Joseph

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Overriding generic methods

Marco van de Voort
In our previous episode, Ryan Joseph said:
> type
> TObjectListAbstract = specialize TList<TObject>;
>   TObjectList = class (TObjectListAbstract)
> procedure Add (value: T); override;
> end;

After specialization, the method signature now substitutes tobject for T, so
try


{$mode objfpc}
type
  generic TList<T> = class (TObject)
                procedure Add (value: T); virtual;
        end;


type
        TObjectListAbstract = specialize TList<TObject>;
        TObjectList = class (TObjectListAbstract)
                procedure Add (value: TObject); override;
        end;

procedure TList.Add (value: T);
begin
end;

procedure tobjectlist.add(value: TObject);
begin
end;

begin
end.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Overriding generic methods

Ryan Joseph
Of course, I should have known. I’m just recently getting started on generics and finding some uses for them. Thanks.

> On Feb 10, 2017, at 3:22 PM, Marco van de Voort <[hidden email]> wrote:
>
> After specialization, the method signature now substitutes tobject for T, so
> try

Regards,
        Ryan Joseph

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal