Recent

Author Topic: Pointer in Generics  (Read 729 times)

Okoba

  • Sr. Member
  • ****
  • Posts: 392
Pointer in Generics
« on: January 04, 2023, 05:09:40 pm »
Hello,

Why the "TPointer<T> = ^T;" raises an error? And what is the correct way?
Also when I write "TPointer<T> = ^T;", FPC raise the error at line of Test1, and it most probably is a bug.

Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. {$mode delphi}
  4.  
  5. type
  6.   TArray<T> = array of T;
  7.   TPointer<T> = ^T;
  8.  
  9.   procedure Test1<T>(A: TArray<T>);
  10.   begin
  11.  
  12.   end;
  13.  
  14.   procedure Test2<T>(A: TPointer<T>);
  15.   begin
  16.  
  17.   end;
  18.  
  19. begin
  20. end.                          

PascalDragon

  • Hero Member
  • *****
  • Posts: 4967
  • Compiler Developer
Re: Pointer in Generics
« Reply #1 on: January 04, 2023, 10:50:14 pm »
Why the "TPointer<T> = ^T;" raises an error? And what is the correct way?

Generic pointers are not supported. (Though I am playing with the tought to support them considering that arrays are supported as well).

As a workaround you can use a type inside a generic record:

Code: Pascal  [Select][+][-]
  1. {$mode delphi}
  2. {$modeswitch advancedrecords}
  3.  
  4. type
  5.   TPointer<T> = record
  6.   type
  7.     PT = ^T;
  8.   end;
  9.  
  10. procedure Test1<T>(A: TPointer<T>.PT);
  11. begin
  12.   Writeln(HexStr(A));
  13. end;
  14.  
  15. var
  16.   l: LongInt;
  17.   p: PLongInt;
  18. begin
  19.   p := @l;
  20.   Test1<LongInt>(p);
  21. end.

Also when I write "TPointer<T> = ^T;", FPC raise the error at line of Test1, and it most probably is a bug.

Please report that as a bug.

Okoba

  • Sr. Member
  • ****
  • Posts: 392
Re: Pointer in Generics
« Reply #2 on: January 05, 2023, 10:28:08 am »
Thank you. Is there an issue for Generic pointers so I can subscribe too?

Okoba

  • Sr. Member
  • ****
  • Posts: 392
Re: Pointer in Generics
« Reply #3 on: January 05, 2023, 12:28:18 pm »
Trying your solution more in a unit, there is a problem:
Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. {$mode delphi}
  4. {$modeswitch advancedrecords}
  5.  
  6. uses unit1;
  7.  
  8. begin
  9.   p := @l;
  10.   Test1<Longint>(p);
  11. end.          
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode delphi}
  4. {$modeswitch advancedrecords}
  5.  
  6. interface
  7.  
  8. type
  9.   TPointer<T> = record
  10.   type
  11.     PT = ^T;
  12.   end;
  13.  
  14. procedure Test1<T>(A: TPointer<T>.PT);
  15.  
  16. implementation
  17.  
  18. procedure Test1<T>(A: TPointer<T>.PT); //unit1.pas(18,11) Error: Function header doesn't match the previous declaration "Test1$1(TPointer$1$crcBAA2E09C_crcBD58B68D.PT);"
  19. begin
  20.   Writeln(HexStr(A));
  21. end;
  22.  
  23. end.                                                                                                            

Why?
« Last Edit: January 05, 2023, 12:42:08 pm by Okoba »

PascalDragon

  • Hero Member
  • *****
  • Posts: 4967
  • Compiler Developer
Re: Pointer in Generics
« Reply #4 on: January 05, 2023, 07:33:47 pm »
Thank you. Is there an issue for Generic pointers so I can subscribe too?

No, there is not.

Why?

Hmm... bug... please report. ::)

Okoba

  • Sr. Member
  • ****
  • Posts: 392
Re: Pointer in Generics
« Reply #5 on: January 07, 2023, 07:29:20 am »
Reported as #40090 and #40091.

PascalDragon

  • Hero Member
  • *****
  • Posts: 4967
  • Compiler Developer
Re: Pointer in Generics
« Reply #6 on: January 08, 2023, 04:15:19 pm »
Thank you.

 

TinyPortal © 2005-2018