Forum > General

Question about generics

(1/1)

alpine:
Is it possible to define a type in the following way:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---type  generic TGMyList<_T> = class  public    type      TItemClass = class of _T; // <-- Error: class type expected, but got "TGMyList$1._T"    ...    procedure Add(Item: _T);  end;   TMyItem = class    constructor Create;  end;   TMyList = specialize TGMyList<TMyItem>;  MyList: TMyList; 
and then:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---  MyList.Add(TMyList.TItemClass.Create); 

marcov:
No. TItemclass is not a variable but a type.

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

alpine:

--- Quote from: marcov 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 )

--- End quote ---

Well, it looks good that way:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---type  generic TGMyList<_T> = class  public    type      TItemClass = _T;  ...
but it feels like slight syntax inconsistency considering:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---type  ...  TClass  = class of tobject;  ...
at the top level.

Warfley:
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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---type generic TGMyList<_T: TObject> = class  type TTClass = class of T;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:
@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>.

Navigation

[0] Message Index

Go to full version