Forum > General
How to create variable sized arrays within a generic record at compile time
jamie:
Consider this
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---Generic TmyArray<T,V>= Record A:Array[0..V] of T;End;
and later on..
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---TMyRecord = Record some_Fixed_Fields:... AVariableTypeAndSizeArray :TmyArray<Dword, 10> // 10 would be 10 octives/Slices of type Dword someOtherFiledsMaybe... End;
I want this at compile time so I can create variable sizes of these records and also they will have class functions etc.
I don't need to use generics but would like a way to indicate the size of the array from just outside the inners of the record so I can have the compiler construct a record of each with variable sizes.
I know this is most likely out of the ordinary, but it seems I've seen this somewhere in my coding career.
Peacoor:
see https://wiki.freepascal.org/FPC_New_Features_Trunk#Support_for_constant_parameters_in_generics
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---Generic TmyArray<T; const V:Integer>= Record A:Array[0..V-1] of T;End;
Thaddy:
You can use length for the number of elements (duh)
Also have a look at DynArrayBounds and DynArrayDim in system.
With these you can retrieve all the information needed about a dynamic array at runtime.
But the above code is nice if you want to uses static arrays and works pretty good. Needs trunk, though.
jamie:
--- Quote from: Peacoor on June 30, 2024, 07:40:48 am ---see https://wiki.freepascal.org/FPC_New_Features_Trunk#Support_for_constant_parameters_in_generics
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---Generic TmyArray<T; const V:Integer>= Record A:Array[0..V-1] of T;End;
--- End quote ---
Ok, so what's the hold up?
I would have thought this simple operation of a Generic would have been there long ago!
Makes it difficult to translate C/C++ code. :(
Thaddy:
The part is the const, which is unique to fpc.
you can also do this in trunk:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---{$mode delphi}type TmyArray<T; const l,h:Integer>= Record A:Array[l..h-1] of T; end; var anArray = TMyArray<integer,3,15>;where both low and high can be defined as const, probably more dimensions as well. It is a real, actually untyped const, not a true integer. See the feature announcement.
All this is at compile time.
Navigation
[0] Message Index
[#] Next page