Forum > General
How to make a specialized TList that holds a list of records?
Cascade:
I’m trying to create a ProjectManager (advanced record) to manage a list of Project records.
--- 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";}};} ---interface uses … Fgl … ; {$mode ObjFPC}{$H+}{$MODESWITCH AdvancedRecords}… TProject=recordprivate … class operator Initialize(var Instance: TProject); class operator Finalize(var Instance: TProject); end; TProjectManager=recordpublic type TProjectList = specialize TFPGList<TProject>;private FProjects: TProjectList; class operator Initialize(var Instance: TProjectManager); class operator Finalize(var Instance: TProjectManager); …end;
When compiling I receive an error on line 1000 of the Fgl unit:
--- 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 TFPGList.IndexOf(const Item: T): Integer;… while (Result < FCount) and (PT(FList)[Result] <> Item) do Error: Operator is not overloaded: “<record type>” = “<record type>”
Lazarus 3.99, FPC 3.3.1 - Can generics be specialized for records as well as classes? If yes, what am I doing wrong here?
TRon:
--- Quote from: Cascade on November 11, 2024, 04:23:02 pm ---If yes, what am I doing wrong here?
--- End quote ---
Not doing anything wrong but an omission (as the compiler tells).
Add the record operator =, see also documentation
Cascade:
Thanks TRon, good to know it's possible. I've read the linked page of documentation, but I don't yet know how or where to add a record operator, nor what adding it achieves (other than making the compiler happy :)). That page links to further pages on the subject of operator overloading, so I've more to read and get my head around.
TRon:
--- Quote from: Cascade on November 11, 2024, 04:55:33 pm ---but I don't yet know how or where to add a record operator, nor what adding it achieves (other than making the compiler happy :)).
--- End quote ---
You define the class operator inside your advanced record (as per example in the documentation) same as you management operators.
Consider the following: the list is able to compare items. Your items are records. How is the list suppose to know what records are equal ? Should the list invents it s own criteria for comparison or would the user rather be in control of that ? ;)
Cascade:
I guess I'd expect no record to equal another, since they are value based rather than reference based, so unique. I suppose if an SHA1 hash of the bytes of a record's allocated memory matched the SHA1 hash of another record's memory, then they could be considered equal (?)
I've speculatively added:
--- 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";}};} ---class operator TProjectManager.=(a,b: TProjectManager): TProjectManager;begin Result := a; end;
...and similar to the TProject definition, just to see whether I could get it to compile - but no joy yet. Perhaps I should switch back to using classes, but I like the tantalising glimpse of automatic life-cycle management that advanced records seem to offer, so thought I'd experiment, and hopefully learn something in the process.
Navigation
[0] Message Index
[#] Next page