Recent

Author Topic: Specializing a generic of generic  (Read 732 times)

Okoba

  • Hero Member
  • *****
  • Posts: 572
Specializing a generic of generic
« on: December 14, 2024, 01:05:48 pm »
Is there anyway I can achieve this with current FPC?
I want to link this types together, so when I specialize TTest3 with for example integer, it I get a TTest2<TTest3<Integer>> in Test2 and TTest1<TTest2<TTest3<Integer>>> in Test1.
Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3.   {$modeSwitch advancedRecords}
  4.  
  5. type
  6.   generic TTest1<T> = record
  7.  
  8.   end;
  9.  
  10.   generic TTest2<T> = record
  11.   type
  12.     TThis = specialize TTest2<T>; //Error: Generics cannot be used as parameters when specializing generics
  13.   var
  14.     Test1: specialize TTest1<TThis>;
  15.   end;
  16.   generic TTest3<T> = record
  17.   type
  18.     TThis = specialize TTest3<T>;
  19.   var
  20.     Test2: specialize TTest2<TThis>;
  21.   end;                                            

ALLIGATOR

  • New Member
  • *
  • Posts: 47
Re: Specializing a generic of generic
« Reply #1 on: December 14, 2024, 02:36:52 pm »
Everything is OK in Delphi 12

Code: Pascal  [Select][+][-]
  1. program test;
  2. {$IFDEF FPC}{$mode delphi}{$ENDIF}
  3.  
  4. type
  5.   GFirst<T> = class
  6.   type
  7.     TThis = GFirst<T>;
  8.   end;
  9.   GSecond<T> = class
  10.   type
  11.     TThis = GSecond<T>;
  12.     TFirstThis = GFirst<TThis>;
  13.   end;
  14.  
  15. begin
  16. end.
« Last Edit: December 14, 2024, 03:52:59 pm by ALLIGATOR »

Okoba

  • Hero Member
  • *****
  • Posts: 572
Re: Specializing a generic of generic
« Reply #2 on: December 14, 2024, 04:09:47 pm »
Adding TTest3 creates the problem, please add that and try again.

ALLIGATOR

  • New Member
  • *
  • Posts: 47
Re: Specializing a generic of generic
« Reply #3 on: December 14, 2024, 04:27:22 pm »
Delphi is fine
In FPC the code above gives an error like yours, but in Delphi everything is OK
But this original code adapted for Delphi compiles successfully in Delphi as well

Code: Pascal  [Select][+][-]
  1. program test;
  2.  
  3. type
  4.   TTest1<T> = class
  5.   end;
  6.  
  7.   TTest2<T> = class
  8.   type
  9.     TThis = TTest2<T>; //Error: Generics cannot be used as parameters when specializing generics
  10.   var
  11.     Test1: TTest1<TThis>;
  12.   end;
  13.   TTest3<T> = class
  14.   type
  15.     TThis = TTest3<T>;
  16.   var
  17.     Test2: TTest2<TThis>;
  18.   end;
  19.  
  20.   begin
  21.   end.

It seems to be a compiler bug
« Last Edit: December 14, 2024, 04:29:05 pm by ALLIGATOR »

TRon

  • Hero Member
  • *****
  • Posts: 3760
Re: Specializing a generic of generic
« Reply #4 on: December 14, 2024, 04:40:38 pm »
@Okoba

Like this ?
Code: Pascal  [Select][+][-]
  1. program test;
  2.  
  3. {$mode objfpc}{$h+}
  4. {$modeSwitch advancedRecords}
  5.  
  6. type
  7.   generic TTest1<T> =
  8.   record
  9.   end;
  10.  
  11.   generic TTest2<T> =
  12.   record
  13.   type
  14.     TThis = T;
  15.   var
  16.     Test1: specialize TTest1<TThis>;
  17.   end;
  18.   generic TTest3<T> = record
  19.   type
  20.     TThis = T;
  21.   var
  22.     Test2: specialize TTest2<TThis>;
  23.   end;
  24.  
  25. begin
  26. end.
  27.  
I do not have to remember anything anymore thanks to total-recall.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5796
  • Compiler Developer
Re: Specializing a generic of generic
« Reply #5 on: December 14, 2024, 04:46:58 pm »
Delphi is fine
In FPC the code above gives an error like yours, but in Delphi everything is OK
But this original code adapted for Delphi compiles successfully in Delphi as well

Please note that Okoba's code uses record and not class. In that case the code won't compile in Delphi either.

It seems to be a compiler bug

While there is a bug regarding this, in the end it is very likely that it will not work anyway, because if T in TTest1<> is used as a field the code will fail due to TTest2<> not being completely defined at the point of specialization inside TTest2<>. Generics can not be used to circumvent the need of proper declaration order.

@Okoba

Like this ?

Please compare your code to what Okoba wants. They lead to different behavior (assuming Okoba's code would work).

Okoba

  • Hero Member
  • *****
  • Posts: 572
Re: Specializing a generic of generic
« Reply #6 on: December 14, 2024, 05:26:07 pm »
@ALLIGATOR Yes it seems.
@TRon No, you are specializing TThis  = T, but I want TThis = TTest3<T>;
@PascalDragon Is there anyway I could archive something like this? I want to have a sub generic type, that calls it's parent. It is done it Test2, but I can not go on with Test3.

 

TinyPortal © 2005-2018