Delphi Anonymous Methods: use them when nothing else is nearly as good, ideal for passing code when you need to parameterize types and procedures by code or behaviour. You can also "simulate" new syntax constructs defined entirely in libraries. Don’t use them when for/in or an equivalent loop would do.
声明:
type
// method reference
TProc = reference to procedure(x: Integer);
procedure Call(const proc: TProc);
begin
proc(42);
end;
使用:
var
proc: TProc;
begin
// anonymous method
proc := procedure(a: Integer)
begin
Writeln(a);
end;
Call(proc);
readln
end.