Sign in Join

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

Mailing lists / fpc-devel / 2019-09

objfpc generics how to generate self type reference ?

4 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

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

Marco van de Voort

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?

Reply by email

Mattias Gaertner
to Marco van de Voort
Show quoted text 11 lines
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>;
Show quoted text 20 lines
          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 ;
Show quoted text 11 lines
     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

Reply by email

Mattias Gaertner
to Mattias Gaertner
Show quoted text 19 lines
On Tue, 3 Sep 2019 12:15:50 +0200 Marco van de Voort 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>;
Btw, mode objfpc allows shortcut THashmapIterator without  as
shortcut for "specialize THashmapIterator<TKey, TValue, T, TTable>";

Mattias

Reply by email

Marco van de Voort
to Mattias Gaertner

Op 2019-09-03 om 13:11 schreef Mattias Gaertner via fpc-devel:

Show quoted text 7 lines
> Inside the generic the templates are normal types: > >       TIteratorType = specialize THashmapIterator<TKey, TValue, T, > TTable>; Btw, mode objfpc allows shortcut THashmapIterator without as shortcut for "specialize THashmapIterator<TKey, TValue, T, TTable>";

It works, thanks. I'm sure I tried it, but probably some editing mishap obscured the result.

This is better than reworking to nested classes because it is backwards compat. (though gvector does use nested class as iterator)

Reply by email