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.
type
generic tgenrec<T, N> = record
data: array[0..N-1] of T;
end;
var
myrec100: specialize tgenrec<dword, 100>; // project1.lpr(7,43) Error: Error in type definition
myrec500: specialize tgenrec<dword, 500>; // project1.lpr(8,43) Error: Error in type definition
begin
end.
Maybe this is possible, only I dont know about it
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().
type
arr100 = array[1..100] of byte;
arr500 = array[1..500] of byte;
arr1000 = array[1..1000] of byte;
arr50000 = array[1..50000] of byte;
arr275000 = array[1..275000] of byte;
generic tgenrec<T, N> = record
data: array[0..sizeof(N)-1] of T;
end;
var
myrec100: specialize tgenrec<byte, arr100>;
myrec500: specialize tgenrec<byte, arr500>;
begin
writeln('sizeof myrec100 = ', sizeof(myrec100));
writeln('sizeof myrec500 = ', sizeof(myrec500));
writeln;
writeln('sizeof custom genrec = ', sizeof( specialize tgenrec<byte, arr100> ));
writeln('sizeof custom genrec = ', sizeof( specialize tgenrec<byte, arr500> ));
writeln('sizeof custom genrec = ', sizeof( specialize tgenrec<byte, arr1000> ));
writeln('sizeof custom genrec = ', sizeof( specialize tgenrec<byte, arr50000> ));
writeln('sizeof custom genrec = ', sizeof( specialize tgenrec<byte, arr275000> ));
readln;
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()