Search code examples
delphigenericsdelphi-xe2

Why does the declaration of a TDictionary with a type as key throw an internal L2095 error?


Would anyone know why the following code throws an internal L2095 error?

Apparently the compiler has a problem with the dictionary's key being a class type.

Anyone know of a work-around?

(PS: the error isn't thrown when I don't include the TestDict := TDictTest.Create; portion or when the key is something else than a class type)

  TTestClass = class
  end;

  TTypeOfTestClass = type of TTestClass;

  TDictTest = TDictionary<TTypeOfTestClass,Integer>;

var
  TestDict: TDictTest;

implementation

initialization

  TestDict := TDictTest.Create;
  TestDict.Free

Added after answer:

Apparently this is about me having instinctively used "type of" instead of "class of". "Type of" is accepted by the compiler, hence the confusion. A reference to a question related to this: What "type of" declaration represents in Delphi and how can it be used


Solution

  • I don't think "type of ..." is even allowed. Use

    TTypeOfTestClass = class of TTestClass;
    

    instead.