[fpc-devel] objfpc generics how to generate self type reference ?

Mattias Gaertner nc-gaertnma at netcologne.de
Tue Sep 3 12:38:49 CEST 2019


On Tue, 3 Sep 2019 12:15:50 +0200
Marco van de Voort <fpc at pascalprogramming.org> wrote:

> I'm trying to let an iterator implement the enumerator pattern and 
> return itself, but for that I need to use the type of the generic:
> 
>       generic THashmapIterator<TKey, TValue, T, TTable>=class
>           public
>            type PValue=^TValue;
> 
>                  // make type alias
> 
>                    TIteratorType = generic THashmapIterator<TKey, 
> TValue, T, TTable>;

Inside the generic the templates are normal types:

      TIteratorType = specialize THashmapIterator<TKey, TValue, T, TTable>;


> 
>            var
>             Fh,Fp:SizeUInt;
>             FData:TTable;
>             function Next:boolean;inline;
>             function MoveNext:boolean;inline;
>             function Prev:boolean;inline;
>             function GetData:T;inline;
>             function GetKey:TKey;inline;
>             function GetValue:TValue;inline;
>             function GetMutable:PValue;inline;
>             procedure SetValue(value:TValue);inline;
>             function getenumerator : TIteratorType ;
>             property Data:T read GetData;
>             property Key:TKey read GetKey;
>             property Value:TValue read GetValue write SetValue;
>             property MutableValue:PValue read GetMutable;
>             property Current : T read GetData;
>         end;
> 

No repetition of templates in mode objfpc:

     function THashmapIterator.getenumerator : TIteratorType ;
 
>       begin
> 
>          result:=self;
> 
>       end;
> 
> but the I can't find a TIteratorType definition that is accepted. Can 
> some of the objfpc generics buffs reveal the secret how to do this?
> 
> Do I need to nest the iterators first, like in Delphi, and if so will 
> ($modeswitch advancedrecords} allow this?


Mattias


More information about the fpc-devel mailing list