TFPGMap : use an object as key lazarus / 21-General ---------------------------------------------------------------------- Neuromancer · 2021-07-04 21:27 Hello, Is it possible to use an object as key with  a TFPGMap, in objfpc or dephi mode ? Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i TCommodity = class    public        FName : String;        constructor Create(AName : String);    end;     TCommodityDictionary = specialize TDictionary; I get the following error : QuoteError: Operator is not overloaded: "TCommodity" < "TCommodity" I tried to overload the < and > operators, but it doesn't work. I use FPC 3.0.4. ---------------------------------------------------------------------- PascalDragon · 2021-07-05 07:05 Quote from: Neuromancer on July 04, 2021, 11:27:13 pmIs it possible to use an object as key with  a TFPGMap, in objfpc or dephi mode ? Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i TCommodity = class    public        FName : String;        constructor Create(AName : String);    end;     TCommodityDictionary = specialize TDictionary; I assume you mean TFPGMap instead of TDictionary? Cause with TDictionary it should work and you should also be able to specify your own comparator if you don't want simple pointer equivalence. Quote from: Neuromancer on July 04, 2021, 11:27:13 pmI get the following error : QuoteError: Operator is not overloaded: "TCommodity" < "TCommodity" Correct. Keys for TFPGMap need to be orderable, thus they need to support the <-operator. Quote from: Neuromancer on July 04, 2021, 11:27:13 pmI tried to overload the < and > operators, but it doesn't work. Also correct, because generics do not support global operator overloads. Your possible solutions: don't use a class instance as keywrap the class instance inside a record that provides a suitable operator overloaduse Generics.Collections.TDictionary<,> and provide a suitable comparator Quote from: jamie on July 05, 2021, 01:34:17 amCode: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   TForm1 = class(TForm)    Button1: TButton;    procedure Button1Click(Sender: TObject);  private   public   end;TMyMap = specialize TFpgMap;var  Form1: TForm1;  MyMap:TMyMap; implementation {$R *.lfm} { TForm1 } procedure TForm1.Button1Click(Sender: TObject);begin   MyMap := TMyMap.Create;   MyMap.Add(0,Form1);   MyMap.Free;end;   as you can see, you need to pass in instances. I suppose you could also specify TClass too, I didn't bother to try.. Neuromancer wants to use the instance as key, and not as value. That are different use cases! Quote from: jamie on July 05, 2021, 03:26:05 amI wanted to add, you can use a POINTER as the type for a key, that will accept objects. While this is correct this might not be what Neuromancer wants. First of it sacrifices type safety and second it does not compare the contents of the TCommunity instances, but their pointers. This might or might not be desirable. ---------------------------------------------------------------------- Neuromancer · 2021-07-05 19:38 Hello, Thanks for your answers. Quote from: PascalDragon on July 05, 2021, 09:05:04 amI assume you mean TFPGMap instead of TDictionary? Cause with TDictionary it should work and you should also be able to specify your own comparator if you don't want simple pointer equivalence. Yes, My TDictionary is basically a wrapper over TFPGMap that provides some of the same features as the Delphi class with the same name. I don't know Generics.Collections.TDictionary. Quote from: jamie on July 05, 2021, 03:26:05 amI wanted to add, you can use a POINTER as the type for a key, that will accept objects. The following works : Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i         TCommodity = class        public            FName : String;          constructor Create(AName : String);        end;         PCommodity = ^TCommodity;         TCommodityDictionary = specialize TDictionary;  //...procedure TAgent.Give(ACommodity : TCommodity; AAmount : Integer);begin    FCommodities.Add(@ACommodity, AAmount); //FCommodities is a TCommodityDictionaryend;   In your opinion, could it be the right approach ? I could also use a record like this : Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i         TCommodity = record        public            FName : String;        end;         PCommodity = ^TCommodity;         TCommodityDictionary = specialize TDictionary;  //...procedure TAgent.Give(ACommodity : PCommodity; AAmount : Integer);begin    FCommodities.Add(ACommodity, AAmount);end; ---------------------------------------------------------------------- PascalDragon · 2021-07-06 13:22 Quote from: Neuromancer on July 05, 2021, 09:38:54 pmQuote from: PascalDragon on July 05, 2021, 09:05:04 amI assume you mean TFPGMap instead of TDictionary? Cause with TDictionary it should work and you should also be able to specify your own comparator if you don't want simple pointer equivalence. Yes, My TDictionary is basically a wrapper over TFPGMap that provides some of the same features as the Delphi class with the same name. I don't know Generics.Collections.TDictionary. Generics.Collections.TDictionary<,> is is the name of the Delphi class and since FPC 3.2.0 it's part of FPC as well. Quote from: Neuromancer on July 05, 2021, 09:38:54 pmIn your opinion, could it be the right approach ? I could also use a record like this : It depends on what you want: if you're satisfied if two TCommunity are equal if the pointers to them are equal, then yes, your approach works (though see below for a better solution for the class-based one). If however you want two TCommunity be equal if they have the same content then the approach using pointers is wrong. For classes you don't need to declare a pointer type, cause classes themselves already are pointers, thus you can do the following: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i TCommodityDictionary = specialize TDictionary; // somewhere else:FCommodities.Add(Pointer(ACommodity), AAmount)); In principle you could also declare a type helper for your TCommodityDictionary to get some type safety back... All in all I'd suggest Generics.Collections.TDictionary<,> however.