Forum > Suggestions

what about an indexed record = irecord type ?

(1/4) > >>

JoLt:
Working with records for a first Pascal project I'm stumped by the realisation i cannot just write out all record fields at once.
 
--- 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";}};} ---...type     myrecord = record        rec : String;        type : Integer;        note : String;    end;... function myfunction : myrecord... begin     writeln(myfunction());  end.  Assuming this is because records are not indexed I came up with this request since I have no need for a variant record or an object or a class.

Please let me know if this makes sense or if I'm better off using a different type anyway.
The choice for a record type here was made because the fields need to be of different types.

Best

JL

LemonParty:
Pascal can't automatically serialize records like JS. Instead you have to implement additional function 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";}};} ---uses   SysUtils; function ToString(const R: myrecord): String;begin  Result:= R.rec + ' ' + IntToStr(R.fType) + ' ' + R.note;end; Then you do:

--- 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";}};} ---{...}function myfunction : myrecord;{...}begin  Writeln(ToString(myfunction));end. 

LemonParty:
Also it is important to clean up your record if you have managed types as strings in it. It can be done this way:

--- 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";}};} ---procedure Free(var R: myrecord);begin  R.rec:= '';  R.note:= '';end; And then:

--- 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";}};} ---{...}function myfunction : myrecord;{...}var  R: myrecord;begin  R:= myfunction;  Writeln(ToString(R));  Free(R);end. 

bytebites:

--- Quote from: LemonParty on September 17, 2025, 05:00:52 pm ---Also it is important to clean up your record if you have managed types as strings in it. It can be done this way:

--- 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";}};} ---procedure Free(var R: myrecord);begin  R.rec:= '';  R.note:= '';end; And then:

--- 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";}};} ---{...}function myfunction : myrecord;{...}var  R: myrecord;begin  R:= myfunction;  Writeln(ToString(R));  Free(R);end. 
--- End quote ---
Absolutely unnecessary to free long string manually.

LemonParty:

--- Quote ---Absolutely unnecessary to free long string manually.
--- End quote ---

Look how many memory consume this program:

--- 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 objfpc}{$H+} uses  SysUtils; type  TRec = record    A, B: String;  end; function FillString: TRec;begin  Result.A:= 'String ' + IntToStr(Random($FFFF));  Result.B:= 'String ' + IntToStr(Random($FFFF));end; const  Size = 1024 * 1024 * 32;var  RecArr: array of TRec;  i: Integer;begin  SetLength(RecArr, Size);  for i:= 0 to Size do    RecArr[i]:= FillString;  SetLength(RecArr, 0);{release records without freeing}  Writeln('Done');  Readln;end.

Navigation

[0] Message Index

[#] Next page

Go to full version