Jump to content
balabuev

Records as TDictionary keys

Recommended Posts

Did you know that the following will automatically work with TDictionary:

type
  TMyKey = record
  public
    class operator Equal(const A, B: TMyKey): Boolean;
    function GetHashCode: Integer;
  end;

Without the need to write custom equality comparer. At least in Delphi 12 it works. And System.TypeIfo unit contains GetRecCompareAddrs function, which is used in default equality comparer implementation. I realized it only recently, and see no docs/info on the internet.

  • Thanks 5

Share this post


Link to post
15 hours ago, balabuev said:

Did you know that the following will automatically work with TDictionary:


type
  TMyKey = record
  public
    class operator Equal(const A, B: TMyKey): Boolean;
    function GetHashCode: Integer;
  end;

Without the need to write custom equality comparer. At least in Delphi 12 it works. And System.TypeIfo unit contains GetRecCompareAddrs function, which is used in default equality comparer implementation. I realized it only recently, and see no docs/info on the internet.

 

Hey, that's actually really cool!

 

Share this post


Link to post

 

On 6/24/2024 at 6:28 PM, balabuev said:

Did you know that the following will automatically work with TDictionary:

This will no longer work by default in Delphi 12.2.

You would need to add to your code:

  System.TypInfo.GetRecCompareAddrs := System.TypInfo.GetRecordCompareAddrs;

 

Also using {$WEAKLINKRTTI ON} no longer causes crashes.   It just results in using the default equality and GetHashCode functions.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×