Forum > General

Error with generic class

(1/2) > >>

Neuromancer:
Hello !

I have the following class declaration :

--- 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";}};} ---    generic TGObjectList<T> = class(specialize TFPGObjectList<T>)        function Contains(const AValue: T): Boolean;        procedure AddRange(const AValues: array of T);    end;
When I try to compile, I get the following errors :

--- Quote ---generics.collections.pas(26,63) Error: class type expected, but got "TGObjectList$1.T"
generics.collections.pas(26,65) Error: class type expected, but got "<erroneous type>"
--- End quote ---

I use FPC 3.2.0. It worked well with the olders versions.

Neuromancer:
I found the solution :

--- 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";}};} ---generic TGObjectList<T: TObject> = class(specialize TFPGObjectList<T>)        function Contains(const AValue: T): Boolean;        procedure AddRange(const AValues: array of T);    end;

Thaddy:
Are you really mixing two or even three different generics frameworks?
1. FGL, as per the specialization
2. RTL-Generics, as per the error message
3. FCL-STL, probably, because that clashed with your use of G (To my eyes)

I wonder how that worked in the past.... It is not wrong - to the compiler - in principle, provided you keep an eye on unit order and/or use unit name prefixes, but it can confuse you.

Note that both methods that you implement are already covered by RTL-Generics in Tlist<T> so they are superflous when you use rtl-generics. The generics.collections.Tlist<T> is the one to use, or one of its descendants for Object support. E.g. Generics.Collections.TObjectList<T> with public contains and public AddRange methods as well as OwnsObjects property.

PascalDragon:

--- Quote from: Thaddy on August 03, 2022, 09:36:07 am ---I wonder how that worked in the past.... It is not wrong - to the compiler - in principle, provided you keep an eye on unit order and/or use unit name prefixes, but it can confuse you.

--- End quote ---

Pre-3.2.0 versions did not have the constraint to TObject, because FGL predated the constraints feature. I only added those with 3.1.1.

Thaddy:
That still does not explain the eclectic mix of generics frameworks.
Actually, he/she was on the right path by using rtl-generics, which already implements the code he/she was trying to write him/her self.
It also may be confusing that rtl-generics is compiled in mode Delphi, but in actual use in other units or programs mode objfpc is legal and can be mixed with libraries in other modes, which is a real nice feature.

Navigation

[0] Message Index

[#] Next page

Go to full version