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

Marco van de Voort fpc at pascalprogramming.org
Tue Sep 3 12:15:50 CEST 2019


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>;

           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;

      function THashmapIterator<TKey, TValue, T, TTable>.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?






More information about the fpc-devel mailing list