One of the interesting aspects of Delphi's implementation of anonymous methods
/closures is the ability to capture the state of variables local to the routine
from which the anon method is called. This is nicely described in one of Marco Cantu's books (Delphi 2009?).

My question is, is there a way to similarly capture the values of variables
which are local to the anonymous method itself?

To hopefully illustrate what I mean, consider the following code snippet,
which does *not* do what I mean:

```
type

  TMemoProc = reference to procedure(Memo: TMemo; StepNo : Integer);

  TForm1 = class(TForm)
  [...]
  private
    procedure Process(Memo: TMemo; StepNo: Integer);
  public
    { Public declarations }
    MemoProc : TMemoProc;
    procedure CreateMemoProc;
  end;

procedure TForm1.Button1Click(Sender: TObject);
var
  i: Integer;
begin
  for i := 1 to 3 do
    MemoProc(Memo1, i);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  CreateMemoProc;
  MemoProc(Memo1, 0);
end;

procedure TForm1.Process(Memo : TMemo; StepNo : Integer);
var
  Count : Integer;
begin
  if StepNo = 0 then
    Count := 1
  else
    Inc(Count);
  Memo.Lines.Add(Self.Name + ' ' +Format('stepno: %d, count: %d', [StepNo, Count]));
end;

procedure TForm1.CreateMemoProc;
begin
  MemoProc :=
    procedure (Memo : TMemo; StepNo : Integer)
    var
      Count : Integer;
    begin
      if StepNo = 0 then
        Count := 1
      else
        Inc(Count);
      Memo.Lines.Add(Format('stepno: %d, count: %d', [StepNo, Count]));
    end;
end;

```

Click Button1 a few times and it's no particular surprise that the numbers inserted
change, as you'd expect from he fact that Delphi doesn't implicitly initialize
simple variables on the stack.

But I'm wondering whether someone can think of an ingenious way to
capture the anon method's local variables between invocations of it.

Fwiw, the reason I'm wondering about this is that there are use-cases where
there need to be typically two invocations of the anon method, one to do some
initialization and one to do some iteration, and the most concise way of coding
would be if the anon method's local variables could somehow be captured.

The sort of use-case I have in mind is writing some general purpose code to iterate a dataset, where it would be handy to specify a boolean function which determines by reference to a row's field values whether a particular dataset row should be processed and another that defines what should be done to the row when the boolean func returns true.

In a tradition implementation, one could specify the fields as DataSet.FieldByName('xxx'), but where many fields and/or many rows are involved, that can be highly inefficient.

So the agenda behind my q is basically to be able to specify the fields involved in an initialization invocation of the anon method, and then to use them directly (as by DataSet11FieldCatflap, rather than DataSet.FieldByName('Catflap'). I have pondered using a containerized list of fields to carry across between the initialization invocation and the itertation one, but it seems "unsatisfying" to me.