Recent

Author Topic: Error with generic class  (Read 1024 times)

Neuromancer

  • New Member
  • *
  • Posts: 44
    • My personal website
Error with generic class
« on: August 03, 2022, 02:36:24 am »
Hello !

I have the following class declaration :
Code: Pascal  [Select][+][-]
  1.     generic TGObjectList<T> = class(specialize TFPGObjectList<T>)
  2.         function Contains(const AValue: T): Boolean;
  3.         procedure AddRange(const AValues: array of T);
  4.     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>"

I use FPC 3.2.0. It worked well with the olders versions.
« Last Edit: August 03, 2022, 05:56:10 am by Neuromancer »

Neuromancer

  • New Member
  • *
  • Posts: 44
    • My personal website
Re: Error with generic class
« Reply #1 on: August 03, 2022, 05:54:29 am »
I found the solution :
Code: Pascal  [Select][+][-]
  1. generic TGObjectList<T: TObject> = class(specialize TFPGObjectList<T>)
  2.         function Contains(const AValue: T): Boolean;
  3.         procedure AddRange(const AValues: array of T);
  4.     end;

Thaddy

  • Hero Member
  • *****
  • Posts: 14359
  • Sensorship about opinions does not belong here.
Re: Error with generic class
« Reply #2 on: August 03, 2022, 09:36:07 am »
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.
« Last Edit: August 03, 2022, 09:46:05 am by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5462
  • Compiler Developer
Re: Error with generic class
« Reply #3 on: August 03, 2022, 10:24:18 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.

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

  • Hero Member
  • *****
  • Posts: 14359
  • Sensorship about opinions does not belong here.
Re: Error with generic class
« Reply #4 on: August 04, 2022, 10:42:33 am »
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.
« Last Edit: August 04, 2022, 10:48:45 am by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11445
  • FPC developer.
Re: Error with generic class
« Reply #5 on: August 04, 2022, 11:20:01 am »
That still does not explain the eclectic mix of generics frameworks.

Well otherwise that it hints on historic origins, iow historic reasons :-)

Thaddy

  • Hero Member
  • *****
  • Posts: 14359
  • Sensorship about opinions does not belong here.
Re: Error with generic class
« Reply #6 on: August 04, 2022, 12:37:02 pm »
Marco, if OP sticked to where the error came from:generics.collections! not FGL, there was no need to write any code.
I can not better my explanation. Look at the error messages, that gives it all away.

It is not historic, but merely confusion.
« Last Edit: August 04, 2022, 12:42:06 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5462
  • Compiler Developer
Re: Error with generic class
« Reply #7 on: August 08, 2022, 11:19:50 pm »
That still does not explain the eclectic mix of generics frameworks.

There is no mixture of frameworks, because Neuromancer simply named their own unit Generics.Collections and declared GObjectList<> there (because TObjectList<> is nowhere near line 26 of its source file ;) ). One can perfectly argue about the sense of doing this (though in times before 3.2.0 there simply was no official Generics.Collections so they might have started their own compatibility unit), but the errors are perfectly explainable.

 

TinyPortal © 2005-2018