Recent

Author Topic: Forward declaration of a generic class  (Read 510 times)

trexet

  • New Member
  • *
  • Posts: 36
Forward declaration of a generic class
« on: June 17, 2024, 07:24:33 pm »
Hi. I'm trying to implement a generic class that would hold a circular reference. As I've found, it should be possible starting from FPC 3.0.

The code:
Code: Pascal  [Select][+][-]
  1. type
  2.  
  3.     { TLC_EL_Connector }
  4.     generic TLC_EL_Connector<T, TLinked: TEntity> = class;
  5.     generic TLC_EL_Connector<T, TLinked: TEntity> = class(TObject)
  6.     published
  7.         LCFrame: TListControlFrame;
  8.     private
  9.         type PTEntityList = ^specialize TEntityList<T>;
  10.         type LinkedConnector = specialize TLC_EL_Connector<TLinked, T>;
  11.     ...
  12.     private
  13.     ...
  14.         Linked: LinkedConnector;
  15.     public
  16.         procedure Link(constref OtherConnector: LinkedConnector);
  17.         procedure AssignData(EList: PTEntityList);
  18.         constructor Create(TheOwner: TWinControl);
  19.         destructor Destroy(); override;
  20.     end;
  21.  
  22. implementation
  23.  

At the implementation line, the complier stops with an Internal Error 200602034. According to FPC source:
Code: Pascal  [Select][+][-]
  1.     function FindUnitSymtable(st:TSymtable):TSymtable;
  2.       begin
  3.         result:=nil;
  4.         repeat
  5.           if not assigned(st) then
  6.            internalerror(200602034);

The error occurs regardless of {$mode ObjFPC} or {$mode delphi}.

FPC 3.2.2, Lazarus 3.4

« Last Edit: June 17, 2024, 07:41:01 pm by trexet »

Thaddy

  • Hero Member
  • *****
  • Posts: 15210
  • Censorship about opinions does not belong here.
Re: Forward declaration of a generic class
« Reply #1 on: June 17, 2024, 09:38:41 pm »
That needs Freepascal trunk.
https://wiki.freepascal.org/FPC_New_Features_Trunk#Support_for_forward_declarations_of_generic_types
FPC 3.2.2 does not have that feature.
Forward declarations are in 3.0 but forward declarations of generic types are only in trunk.
« Last Edit: June 17, 2024, 09:50:58 pm by Thaddy »
Of course the national anthem of the U.S.A. was written by Jimi Hendrix, didn't you know that?

trexet

  • New Member
  • *
  • Posts: 36
Re: Forward declaration of a generic class
« Reply #2 on: June 17, 2024, 11:02:09 pm »
Thanks, managed to overcome this with pointer casting:

Code: Pascal  [Select][+][-]
  1. generic TLC_EL_Connector<T, TLinked: TEntity> = class(TObject)
  2. private
  3.     type PLinkedConnectorObj = ^TObject;
  4. private
  5.     PLinked: PLinkedConnectorObj;
  6.  
  7. ...
  8.  
  9. var Linked: specialize TLC_EL_Connector<TLinked, T>;
  10. begin
  11.     if PLinked = nil then exit;
  12.     Linked := PLinked^ as specialize TLC_EL_Connector<TLinked, T>;

 

TinyPortal © 2005-2018