Below is SSCCE, based on an example in the "Anonymous Methods" section of Part 1 of Chris Rolliston's excellent Delphi XE2 Foundations book, about the idea of ββa variable capture (any errors in it completely depend on me).
It works exactly as I expected, registering 666, 667, 668, 669 with successive clicks of the BtnInvoke Button. In particular, this illustrates well how the captured version of the local variable i is saved after btnSetUpClick exits.
So far so good. The problem I'm asking about is not in this code as such, but what Allen Bauer's blog post says here:
http://blogs.embarcadero.com/abauer/2008/10/15/38876
Now I know better than arguing with the boss, so I'm sure I miss the point about the difference he draws between variable capture and value capture. To my simple way of looking at this, my CR-based example captures the value of I, capturing I as a variable.
So my question is exactly what Mr. Bauer is trying to draw, exactly?
(By the way, despite the fact that I watched the Delphi SO section daily for 9 months, I'm still not completely clear if this is q on the topic. If not, my apologies and I will remove it.)
type TAnonProc = reference to procedure; var P1, P2 : TAnonProc; procedure TForm2.Log(Msg : String); begin Memo1.Lines.Add(Msg); end; procedure TForm2.btnSetUpClick(Sender: TObject); var I : Integer; begin I := 41; P1 := procedure begin Inc(I); Log(IntToStr(I)); end; I := 665; P2 := procedure begin Inc(I); Log(IntToStr(I)); end; end; procedure TForm2.btnInvokeClick(Sender: TObject); begin Assert(Assigned(P1)); Assert(Assigned(P2)); P1; P2; end;
delphi delphi-xe2
MartynA
source share