Recent

Author Topic: TFpgMap.onDataCompare problem  (Read 349 times)

Frogfather

  • New Member
  • *
  • Posts: 49
TFpgMap.onDataCompare problem
« on: December 09, 2022, 09:54:11 pm »
Hello,

I'm experimenting with sorting a string->int map by data value using the onDataCompare function.

So I have defined my map:
TStringIntMap = specialize TFPGMap<String,Integer>;

In the class I want to use this I have

TSomeClass = class
private
fMap:TStringIntMap;
function compare(const d1,d2:integer):integer; 
public
constructor create;
end;

and in the constructor I have

constructor create
begin
fMap:=TStringIntMap.create;
fMap..OnDataCompare:=@compare;
end;

this looks pretty similar to the example but fails to compile with the error

Incompatible type for arg no. 1: Got "<procedure variable type of function(const LongInt;const LongInt):LongInt of object;Register>", expected "TFPGMap$2$crc3DEDFEAF.<procedure variable type of function(const LongInt;const LongInt):LongInt;Register>"

and I'm not sure what that means. What is the difference between 'LongInt of object;Register' and 'LongInt;Register' ?

Anyway - if anyone has any suggestions I'd be most grateful.

Cheers
John

jamie

  • Hero Member
  • *****
  • Posts: 5077
Re: TFpgMap.onDataCompare problem
« Reply #1 on: December 10, 2022, 12:20:41 am »
remove the "@" ?
The only true wisdom is knowing you know nothing

Frogfather

  • New Member
  • *
  • Posts: 49
Re: TFpgMap.onDataCompare problem
« Reply #2 on: December 10, 2022, 06:52:05 am »
Hi Jamie,

That doesn't work either - I think if you're in Delphi mode you pass the method rather than a pointer to it.

bytebites

  • Hero Member
  • *****
  • Posts: 541

Frogfather

  • New Member
  • *
  • Posts: 49
Re: TFpgMap.onDataCompare problem
« Reply #4 on: December 10, 2022, 10:39:34 am »
Hi Bytebytes

I understand how to pass a procedural parameter and have done it successfully with things that aren't genericised - it's just in this particularly instance I'm having difficulty with. As far as I can see the signature of the compare method matches that required by the TFPGMap instance but the compiler disagrees.  :'(


avk

  • Hero Member
  • *****
  • Posts: 686
Re: TFpgMap.onDataCompare problem
« Reply #5 on: December 10, 2022, 11:10:10 am »
I wonder if anything changes if your comparator declaration would be replaced with this one?
Code: Pascal  [Select][+][-]
  1. ...
  2.   fMap:TStringIntMap;
  3.   class function compare(const d1,d2:integer):integer; static;
  4. ...
  5.  
 

Frogfather

  • New Member
  • *
  • Posts: 49
Re: TFpgMap.onDataCompare problem
« Reply #6 on: December 10, 2022, 11:28:02 am »
Hi avk,

That does compile! Thank you.

 

TinyPortal © 2005-2018