Recent

Author Topic: Generic record in record and With  (Read 677 times)

Okoba

  • Hero Member
  • *****
  • Posts: 528
Generic record in record and With
« on: January 07, 2023, 09:32:26 am »
Hello,

In this samples, Test3 raises and error, like FPC does not see R is a record. Is this a bug?
Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3.   {$mode delphi}
  4.  
  5. type
  6.   TTest1 = record
  7.     I: Integer;
  8.   end;
  9.  
  10.   procedure Test1<T>(var V: T);
  11.   begin
  12.     with V do
  13.       WriteLn(I);
  14.   end;
  15.  
  16. type
  17.   TTest2<T> = record
  18.     I: T;
  19.   end;
  20.  
  21.   procedure Test2<T>(var V: T);
  22.   begin
  23.     with V do
  24.       WriteLn(I);
  25.   end;
  26.  
  27. type
  28.   TTest3Record<T> = record
  29.     I: T;
  30.   end;
  31.  
  32.   TTest3<T> = record
  33.     R: TTest3Record<T>;
  34.   end;
  35.  
  36.   procedure Test3<T>(var V: T);
  37.   begin
  38.     with V.R do //Error: Expression type must be class or record type, got <erroneous type>
  39.       WriteLn(I);
  40.   end;
  41.  
  42.   procedure Test3Direct<T>(var V: T);
  43.   begin
  44.     WriteLn(V.R.I);
  45.   end;
  46.  
  47. var
  48.   V1: TTest1;
  49. type
  50.   T2 = TTest2<Integer>;
  51. var
  52.   V2: T2;
  53. type
  54.   T3 = TTest3<Integer>;
  55. var
  56.   V3: T3;
  57. begin
  58.   Test1<TTest1>(V1);
  59.   Test2<T2>(V2);
  60.   Test3<T3>(V3); //Error
  61.   Test3Direct<T3>(V3);
  62. end.

Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
Re: Generic record in record and With
« Reply #1 on: January 07, 2023, 03:16:00 pm »
V.R <> V.R<T>
So
Code: Pascal  [Select][+][-]
  1.  with V.R<T> do
untested!

Also note that the code needs a record constraint for T
« Last Edit: January 07, 2023, 03:26:54 pm by Thaddy »
Specialize a type, not a var.

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: Generic record in record and With
« Reply #2 on: January 07, 2023, 03:39:10 pm »
Hello,

In this samples, Test3 raises and error, like FPC does not see R is a record. Is this a bug?
Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3.   {$mode delphi}
  4.  
  5. type
  6.   TTest1 = record
  7.     I: Integer;
  8.   end;
  9.  
  10.   procedure Test1<T>(var V: T);
  11.   begin
  12.     with V do
  13.       WriteLn(I);
  14.   end;
  15.  
  16. type
  17.   TTest2<T> = record
  18.     I: T;
  19.   end;
  20.  
  21.   procedure Test2<T>(var V: T);
  22.   begin
  23.     with V do
  24.       WriteLn(I);
  25.   end;
  26.  
  27. type
  28.   TTest3Record<T> = record
  29.     I: T;
  30.   end;
  31.  
  32.   TTest3<T> = record
  33.     R: TTest3Record<T>;
  34.   end;
  35.  
  36.   procedure Test3<T>(var V: T);
  37.   begin
  38.     with V.R do //Error: Expression type must be class or record type, got <erroneous type>
  39.       WriteLn(I);
  40.   end;
  41.  
  42.   procedure Test3Direct<T>(var V: T);
  43.   begin
  44.     WriteLn(V.R.I);
  45.   end;
  46.  
  47. var
  48.   V1: TTest1;
  49. type
  50.   T2 = TTest2<Integer>;
  51. var
  52.   V2: T2;
  53. type
  54.   T3 = TTest3<Integer>;
  55. var
  56.   V3: T3;
  57. begin
  58.   Test1<TTest1>(V1);
  59.   Test2<T2>(V2);
  60.   Test3<T3>(V3); //Error
  61.   Test3Direct<T3>(V3);
  62. end.
Have you tested this with recent Delphi ?
The only true wisdom is knowing you know nothing

Okoba

  • Hero Member
  • *****
  • Posts: 528
Re: Generic record in record and With
« Reply #3 on: January 07, 2023, 05:20:22 pm »
@Thaddy, does not work.
@jamie, no, I don't have it.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5446
  • Compiler Developer
Re: Generic record in record and With
« Reply #4 on: January 08, 2023, 04:17:46 pm »
In this samples, Test3 raises and error, like FPC does not see R is a record. Is this a bug?

Please report a bug with a complete example.

Have you tested this with recent Delphi ?

Delphi's generic implementation does not ever remotely support code like that. FPC's implementation is quite a bit more similar to C++'s templates in that regard.

Okoba

  • Hero Member
  • *****
  • Posts: 528
Re: Generic record in record and With
« Reply #5 on: January 08, 2023, 04:21:15 pm »
Reported as #40099.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5446
  • Compiler Developer
Re: Generic record in record and With
« Reply #6 on: January 08, 2023, 04:24:04 pm »

 

TinyPortal © 2005-2018