Recent

Author Topic: Question about generics  (Read 1281 times)

alpine

  • Hero Member
  • *****
  • Posts: 1038
Question about generics
« on: May 05, 2021, 08:14:57 pm »
Is it possible to define a type in the following way:

Code: Pascal  [Select][+][-]
  1. type
  2.   generic TGMyList<_T> = class
  3.   public
  4.     type
  5.       TItemClass = class of _T; // <-- Error: class type expected, but got "TGMyList$1._T"
  6.     ...
  7.     procedure Add(Item: _T);
  8.   end;
  9.  
  10.   TMyItem = class
  11.     constructor Create;
  12.   end;
  13.  
  14.   TMyList = specialize TGMyList<TMyItem>;
  15.   MyList: TMyList;
  16.  

and then:
Code: Pascal  [Select][+][-]
  1.   MyList.Add(TMyList.TItemClass.Create);
  2.  
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: Question about generics
« Reply #1 on: May 05, 2021, 09:21:51 pm »
No. TItemclass is not a variable but a type.

But something similar is possible when you leave out the Class of (TItemClass = T )


alpine

  • Hero Member
  • *****
  • Posts: 1038
Re: Question about generics
« Reply #2 on: May 06, 2021, 03:00:00 am »
No. TItemclass is not a variable but a type.

But something similar is possible when you leave out the Class of (TItemClass = T )

Well, it looks good that way:

Code: Pascal  [Select][+][-]
  1. type
  2.   generic TGMyList<_T> = class
  3.   public
  4.     type
  5.       TItemClass = _T;
  6.   ...

but it feels like slight syntax inconsistency considering:

Code: Pascal  [Select][+][-]
  1. type
  2.   ...
  3.   TClass  = class of tobject;
  4.   ...

at the top level.
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

Warfley

  • Hero Member
  • *****
  • Posts: 1499
Re: Question about generics
« Reply #3 on: May 06, 2021, 04:56:52 am »
If you want to have the "class of ..." construction, you need to add a constraint to your generic definition that the type will indeed be a class:
Code: Pascal  [Select][+][-]
  1. type generic TGMyList<_T: TObject> = class
  2.   type TTClass = class of T;
  3. end;

But for what you are proposing, you don't need the class-of syntax. Class of gives you the type of the class type, which is useful for doing runtime level polymorphism over class types (e.g. calling virtual constructors).
But this only makes sense if you want to default to a common base class, which you do not have in your example. I think you are mixing things up here. Generics as you are using them are completely compile time and static while the "class of" mechanism is a runtime feature allows dynamic polymorphism

alpine

  • Hero Member
  • *****
  • Posts: 1038
Re: Question about generics
« Reply #4 on: May 06, 2021, 12:36:54 pm »
@Warfley
I asked a question through an example. I didn't proposed anything. I wanted to clarify to myself how FPC generics actually works in comparison to templates in other languages.

Nevertheless, thanks for the answer, it gives me some extra clues,<_T:TObject>.
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

 

TinyPortal © 2005-2018