Recent

Author Topic: Generic records with variable array size (generics with types and numerics)  (Read 277 times)

Fibonacci

  • Hero Member
  • *****
  • Posts: 643
  • Internal Error Hunter
What I want to achieve is something like this: to create a generic record and specialize it with a specific number of elements in its array.

Code: Pascal  [Select][+][-]
  1. type
  2.   generic tgenrec<T, N> = record
  3.     data: array[0..N-1] of T;
  4.   end;
  5.  
  6. var
  7.   myrec100: specialize tgenrec<dword, 100>; // project1.lpr(7,43) Error: Error in type definition
  8.   myrec500: specialize tgenrec<dword, 500>; // project1.lpr(8,43) Error: Error in type definition
  9.  
  10. begin
  11. end.

Maybe this is possible, only I dont know about it :D

I managed to come up with a little hack to achieve something that kind of does the job, but is limited to a having a predefined array of bytes due to the use of SizeOf().

Code: Pascal  [Select][+][-]
  1. type
  2.   arr100    = array[1..100]    of byte;
  3.   arr500    = array[1..500]    of byte;
  4.   arr1000   = array[1..1000]   of byte;
  5.   arr50000  = array[1..50000]  of byte;
  6.   arr275000 = array[1..275000] of byte;  
  7.  
  8.   generic tgenrec<T, N> = record
  9.     data: array[0..sizeof(N)-1] of T;
  10.   end;
  11.  
  12. var
  13.   myrec100: specialize tgenrec<byte, arr100>;
  14.   myrec500: specialize tgenrec<byte, arr500>;
  15.  
  16. begin
  17.   writeln('sizeof myrec100 = ', sizeof(myrec100));
  18.   writeln('sizeof myrec500 = ', sizeof(myrec500));
  19.   writeln;
  20.  
  21.   writeln('sizeof custom genrec = ', sizeof( specialize tgenrec<byte, arr100>    ));
  22.   writeln('sizeof custom genrec = ', sizeof( specialize tgenrec<byte, arr500>    ));
  23.   writeln('sizeof custom genrec = ', sizeof( specialize tgenrec<byte, arr1000>   ));
  24.   writeln('sizeof custom genrec = ', sizeof( specialize tgenrec<byte, arr50000>  ));
  25.   writeln('sizeof custom genrec = ', sizeof( specialize tgenrec<byte, arr275000> ));
  26.  
  27.   readln;
  28. end.

What do you guys think about it? Anyone know a better solution?

EDIT: One thing I should mention, I cant use dynamic arrays and SetLength() 8)
« Last Edit: October 17, 2024, 09:23:15 pm by Fibonacci »

PascalDragon

  • Hero Member
  • *****
  • Posts: 5796
  • Compiler Developer
What do you guys think about it? Anyone know a better solution?

In main constant parameters are supported:

Code: Pascal  [Select][+][-]
  1. program tgenconst;
  2.  
  3. type
  4.   generic tgenrec<T; const N: SizeInt> = record
  5.     data: array[0..N-1] of T;
  6.   end;
  7.  
  8. var
  9.   myrec100: specialize tgenrec<dword, 100>; // project1.lpr(7,43) Error: Error in type definition
  10.   myrec500: specialize tgenrec<dword, 500>; // project1.lpr(8,43) Error: Error in type definition
  11.  
  12. begin
  13. end.

In older versions you can only really use the workaround you presented.

Fibonacci

  • Hero Member
  • *****
  • Posts: 643
  • Internal Error Hunter
This is great news, although I will need to upgrade this particular project to use newer version of FPC. Thanks though, didnt know it supported that in the trunk.
« Last Edit: October 17, 2024, 09:44:00 pm by Fibonacci »

 

TinyPortal © 2005-2018