그래서 같은 일반적인 기록을 선언하고 싶습니다 :익명 메소드 유형에 어떤 일반 제약 조건을 사용합니까?
type
TMyDelegate<T: constraint> = record
private
fDelegate: T;
public
class operator Implicit(a: T): TMyDelegate;
class operator Implicit(A: TMyDelegate: T);
end;
내가 reference to procedure/function
에 T
을 제한하고 싶습니다. (가능한 한 많이).
나는이 시도했지만,이 컴파일되지 않습니다 :
[dcc32 Error] Project3.dpr(39): E2514 Type parameter 'T' must support interface 'TProc2'
의 (목록에 제네릭 형식을 제한 할 수있는 방법이 있나요 :
program Project3;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils;
type
TProc1 = reference to procedure(a: Integer);
TProc2 = reference to procedure(b: TObject);
TTest<T: TProc1, TProc2> = record
private
fData: T;
public
class operator Implicit(a: T): TTest<T>;
class operator Implicit(a: TTest<T>): T;
end;
{ TTest<T> }
class operator TTest<T>.Implicit(a: T): TTest<T>;
begin
Result.fData:= a;
end;
class operator TTest<T>.Implicit(a: TTest<T>): T;
begin
Result:= a.fData;
end;
var
Delegate1: TProc1;
Delegate2: TProc2;
var
MyTest1: TTest<TProc1>; <<-- error
MyTest2: TTest<TProc2>;
begin
MyTest1:=
procedure(a: Integer)
begin
WriteLn(IntToStr(a));
end;
end.
이 컴파일 에러 제공) 익명 형식?
제약 조건이 OR'ed가 아닌 AND'ed 인 것을 잊어 버렸습니다. – Johan
참조 절차 제약 조건은 사실 전혀 쓸모가 없습니다. –
요구 사항을 충족하는 THack = class (TInterfacedObject, TProc1, TProc2) 클래스를 만들 수 있습니다. 어떤 용도로 사용할 지 모르겠지만 컴파일 할 수 있다고 생각합니다. – Johan