[fpc-pascal] Strange behavior in generics.collections TDictionary

Thomas Kurz fpc.2021 at t-net.ruhr
Tue Jan 25 18:48:09 CET 2022


Consider the following code:

***
PROGRAM project1;

{$mode objfpc}
{$longstrings on} // see output below
{$modeswitch advancedrecords}

USES Variants, Generics.Collections, SysUtils;

TYPE TRecord = PACKED RECORD
  FID: NativeUInt;
  FKey: String;
  CONSTRUCTOR Create (AID: NativeUInt; AKey: String);
END;

CONSTRUCTOR TRecord.Create (AID: NativeUInt; AKey: String);
BEGIN
  FID := AID;
  FKey := UpperCase (AKey);
END;

VAR
  Dict: SPECIALIZE TDictionary<TRecord,Variant>;
  i: SPECIALIZE TPair<TRecord,Variant>;

BEGIN
  Dict := SPECIALIZE TDictionary<TRecord,Variant>.Create;
  Dict.Add (TRecord.Create (1, 'test'), 1);
  FOR i IN Dict DO Writeln (i.Key.FID, #9, i.Key.FKey, #9, i.Value);
  // ^^^ 1 TEST 1
  // -> so the entry is ok!
  Writeln (Dict.ContainsKey (TRecord.Create (1, 'test')));
  // ^^^ with longstrings on -> FALSE
  //     with longstrings off -> TRUE
  Writeln (Dict.ContainsKey (TRecord.Create (1, 'TEST')));
  // ^^^ always FALSE
  Dict.Free;
END.
***

I'm very confused... I have no idea if I'm overseeing something or this is a bug in generics.collections or in the compiler.

My system is:
Lazarus 2.2.0 (64 bit), FPC 3.2.2 (64 bit) on Windows 8.1 (64 bit)



More information about the fpc-pascal mailing list