Reputation: 10459
In the following code:
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, System.Generics.Collections;
type
TForm1 = class(TForm)
private
{ Private declarations }
public
{ Public declarations }
end;
TMyClass<T: TForm> = class
public
constructor Create;
end;
var
Form1: TForm1;
List: TDictionary<integer, TMyClass<TForm>>;
implementation
{$R *.dfm}
{ TMyClass<T> }
constructor TMyClass<T>.Create;
begin
List.Add(1, self);
end;
end.
I get error:
[dcc32 Error] Unit1.pas(35): E2010 Incompatible types: 'Unit1.TMyClass' and 'Unit1.TMyClass.T>'
in line where I am trying to add Self to the TDictionary. How can I add generic class to the TDictionary, where second parameter takes generic object?
Upvotes: 1
Views: 377
Reputation: 21713
While your constraint is ensuring that T
can only be of TForm
the compiler does not support what is called covariance.
What you can do is hardcast Self
to TMyClass<TForm>
to add it.
Upvotes: 3