数日、誰かがこのコードでエラーが発生する理由を尋ねました。私は理解しようとしましたが、できませんでしたので、皆さんが私たちを助けてくれることを願っています.
最初のユニット: クラスが定義されているユニット Unit1;
interface
type
TSomeGeneric<T> = class
private
function getSelf: TSomeGeneric<T>;
public
property This : TSomeGeneric<T> read getSelf;
end;
implementation
{ TSomeGeneric<T> }
function TSomeGeneric<T>.getSelf: TSomeGeneric<T>;
begin
Result := Self;
end;
end.
unit2 : unit 1 unit Unit2 で定義されたクラスへの参照を作成します。
interface
uses
Unit1;
type
TSomeGeneric<T> = class(Unit1.TSomeGeneric<T>);
implementation
end.
frmmain: unit2 を使用します。あるポイント ユニット ufmMain です。
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs,
Unit2;
type
TfrmMain = class(TForm)
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
procedure SomeProcedure(ASG : TSomeGeneric<Integer>);
public
{ Public declarations }
end;
var
frmMain: TfrmMain;
implementation
{$R *.dfm}
{ TfrmMain }
procedure TfrmMain.FormCreate(Sender: TObject);
var ASG : TSomeGeneric<Integer>;
begin
ASG := TSomeGeneric<Integer>.Create();
with ASG do
try
//make use os the class ...
SomeProcedure(This); //error -> incompatible types
finally
Free();
end;
end;
procedure TfrmMain.SomeProcedure(ASG: TSomeGeneric<Integer>);
begin
//...
end;
end.
この時点で TSomeGeneric = class(Unit1.TSomeGeneric) TSomeGeneric が参照ではなく別のクラスとして定義されているのではないかと疑っていますが、それを確認することはできません