Recent

Author Topic: [SOLVED] How to extend generic class from FGL  (Read 2731 times)

Blaazen

  • Hero Member
  • *****
  • Posts: 3237
  • POKE 54296,15
    • Eye-Candy Controls
[SOLVED] How to extend generic class from FGL
« on: July 25, 2018, 02:03:06 pm »
Hi, question for generic entusiasts: how can I extend generic class from FGL?
Code: Pascal  [Select][+][-]
  1. {$mode objpc}
  2. uses ... FGL, ...
  3. ...
  4. generic TFPGObjectListEx<T> = class (TFPGObjectList<T>)
  5.  
  6. end;
This doesn't work in latest FPC trunk. I got "Error: Generics without specialization cannot be used as a type for a variable". I tried to add "specialize"
 or remove "generic" and both "<T>" but none seem to work.

I just need to add a few methods to TFPGObjectList.
« Last Edit: July 26, 2018, 12:01:58 am by Blaazen »
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

Thaddy

  • Hero Member
  • *****
  • Posts: 14197
  • Probably until I exterminate Putin.
Re: How to extend generic class from FGL
« Reply #1 on: July 25, 2018, 02:47:23 pm »
Did you forgot type?
And why are you using mode objfpc?
Anyway:
Code: Pascal  [Select][+][-]
  1. program untitled;
  2. {$mode objfpc}{$H+}
  3. uses fgl;
  4. type
  5.  generic TFPGObjectListEx<T:Tobject> = class(specialize TFPGObjectList<T>)
  6.  end;
  7. begin
  8. end.
Or:
Code: Pascal  [Select][+][-]
  1. program untitled;
  2. {$ifdef fpc}{$mode delphi}{$H+}{$endif}
  3. uses fgl;
  4. type
  5.   TFPGObjectListEx<T:class> = class(TFPGObjectList<T>)
  6.   end;
  7. begin
  8. end.
You were probably confused that the object list already has a constraint: Tobject (or class)

Hope that helps (I am in a good mood, except for the heat). 8-)

(p.s.: the constraint is a very recent fix by Sven: in 3.0.4 you can skip the constraint, but I would advise against it)
« Last Edit: July 25, 2018, 03:13:28 pm by Thaddy »
Specialize a type, not a var.

Blaazen

  • Hero Member
  • *****
  • Posts: 3237
  • POKE 54296,15
    • Eye-Candy Controls
Re: How to extend generic class from FGL
« Reply #2 on: July 25, 2018, 03:22:51 pm »
Thanks, both works! :)

BTW:
Code: Pascal  [Select][+][-]
  1. function TBaseElFPGObjList.GetItembyIdx(AIdx: Integer): T;
  2. var i: Integer;
  3. begin
  4.   for i:=0 to Count-1 do
  5.     if Items[i].Idx=AIdx then
  6.       begin
  7.         Result:=Items[i];
  8.         exit;  { Exit! }
  9.       end;
  10.   Result:=T(nil);   //<-- is this normal?
  11. end;
I've never seen retyping nil to T. Without it I got "Error: Incompatible types: got "Pointer" expected "$gendef112"".

Anyway, I have to dive deeper to generics, it's fun  :)
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

Thaddy

  • Hero Member
  • *****
  • Posts: 14197
  • Probably until I exterminate Putin.
Re: How to extend generic class from FGL
« Reply #3 on: July 25, 2018, 03:29:05 pm »
Thanks, both works! :)

BTW:
Code: Pascal  [Select][+][-]
  1. function TBaseElFPGObjList.GetItembyIdx(AIdx: Integer): T;
  2. var i: Integer;
  3. begin
  4.   for i:=0 to Count-1 do
  5.     if Items[i].Idx=AIdx then
  6.       begin
  7.         Result:=Items[i];
  8.         exit;  { Exit! }
  9.       end;
  10.   Result:=T(nil);   //<-- is this normal?
  11. end;
I've never seen retyping nil to T. Without it I got "Error: Incompatible types: got "Pointer" expected "$gendef112"".

Anyway, I have to dive deeper to generics, it's fun  :)
This is one of those rare occasions that our benevolent ruler of the universe (Florian) made a very recent "fix" that in my opinion is avoiding the problem.
I am not prepared to discuss the issue, since he decided. :'(
For more info: https://bugs.freepascal.org/view.php?id=33961 and use mantis to see how Florian solved it (as opposed to Michael, who fixed the docs).
My opinion is that is is too relaxed, way too relaxed and goes against type-safety. Docs are OK.
(BTW: beautiful components, again...)

And YES, generics are fun.
(Actually this will confuse people - so if your are not Blaazen, keep your mouth shut- )
« Last Edit: July 25, 2018, 07:05:40 pm by Thaddy »
Specialize a type, not a var.

 

TinyPortal © 2005-2018