Forum > General
How to create variable sized arrays within a generic record at compile time
PascalDragon:
--- Quote from: jamie on June 30, 2024, 03:12:32 pm ---
--- 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?
--- End quote ---
There is no hold up, because the feature already exists in trunk/main/3.3.1.
--- Quote from: jamie on June 30, 2024, 03:12:32 pm ---I would have thought this simple operation of a Generic would have been there long ago!
--- End quote ---
There aren't that many languages that support constants for whatever mechanism they have for generics. Delphi for example doesn't and that is what FPC's generic implementation is mainly driven by (even if it predates the Delphi one).
--- Quote from: jamie on June 30, 2024, 03:12:32 pm ---Makes it difficult to translate C/C++ code. :(
--- End quote ---
Might be, but the goal of the generics is not to be able to translate C++ code (it can't be C code, because C code does not support templates), but to write type safe, generic Pascal code.
Thaddy:
For completeness, you don't need the record notation we all used.
You can simply write it like 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";}};} ---{$mode delphi}type TStaticArray<T; const lowbound,highbound: Integer> = array[lowbound..highbound] of T;var a:TStaticArray<integer,3,14>;begin writeln('number of elements is: ',length(a)); writeln('bounds are: ', low(a):4,high(a):4);end.
Navigation
[0] Message Index
[*] Previous page