我在使用泛型时遇到问题。我不知道如何传递OnCallbackWrapper
给CallbackWrapper
程序。我在以下示例中收到“不兼容的类型”错误:
unit uTest;
interface
uses
Generics.Defaults;
type
TGenericCallback<T> = procedure(Fields: T);
type
TSpecificFields = record
A: Integer;
B: Integer;
C: Integer;
end;
const
SpecificFields: TSpecificFields =
(A: 5; B: 4; C: 3);
procedure CallbackWrapper(GenericCallback: TGenericCallback<TSpecificFields>);
implementation
procedure CallbackWrapper(GenericCallback: TGenericCallback<TSpecificFields>);
begin
GenericCallback(SpecificFields);
end;
procedure OnCallbackWrapper(const Fields: TSpecificFields);
begin
Assert(Fields.A = 5);
Assert(Fields.B = 4);
Assert(Fields.C = 3);
end;
procedure Dummy;
begin
CallbackWrapper(OnCallbackWrapper); //Incompatible types here
end;
end.
我究竟做错了什么?谢谢。