<br><br><div class="gmail_quote">On Tue, Nov 30, 2010 at 12:26 PM, Vincent Snijders <span dir="ltr"><<a href="mailto:vincent.snijders@gmail.com">vincent.snijders@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
2010/11/30 Max Vlasov <<a href="mailto:max.vlasov@gmail.com">max.vlasov@gmail.com</a>>:<br>
<div><div></div><div class="h5">> So, I suppose that implementing closures is not in real plans (it's ok, I<br>
> can live with that), but what about the second, is it possible?<br>
><br>
> I will glad to hear different opinions from the developers<br>
><br>
<br>
</div></div>Nested procedural variables are a new feature in fpc 2.5.1. See:<br>
<a href="http://svn.freepascal.org/cgi-bin/viewvc.cgi?view=rev&revision=15656" target="_blank">http://svn.freepascal.org/cgi-bin/viewvc.cgi?view=rev&revision=15656</a><br>
<br>
I don't know if that is what you are asking for.<br>
<br></blockquote></div><br><br>I'm really impressed. Did a quick test in the fpc ide. It's not just a more elegant way to keep local context, , it's also more universal callback compatible with any destination (local, global, object). If anyone is interested, see it in the example below. <br>
<br>Max<br><br><br>program test;<br><br>{$mode delphi}<br>{$modeswitch nestedprocvars}<br><br>type<br> TCallBackProc = procedure(Value: integer) is nested;<br><br>procedure EnumValues(const Proc: TCallBackProc; Count: integer);<br>
begin<br> while Count >= 0 do<br> begin<br> Dec(Count);<br> Proc(Count)<br> end;<br>end;<br><br> { ************** Callback is a local procedure inside another procedure *************** }<br><br>function EnumUserProc: integer;<br>
var<br> TotalCount: integer;<br><br> procedure CallBackHere(Value: integer);<br> begin<br> Inc(TotalCount, Value);<br> end;<br><br>begin<br> TotalCount:=0;<br> EnumValues(CallBackHere, 10);<br> Result:=TotalCount;<br>
end;<br><br>{ **************** Callback is a procedure (classic) ************* }<br><br>var<br> TotalCount2: integer;<br><br>procedure CallBackHere2(Value: integer);<br>begin<br> Inc(TotalCount2, Value);<br>end;<br><br>
function EnumUserProc2: integer;<br>begin<br> TotalCount2:=0;<br> EnumValues(CallBackHere2, 10);<br> Result:=TotalCount2;<br>end;<br><br>{ ************* Callback is a local procedure inside a method **************** }<br>
<br>type<br> TTestObject = class(TObject)<br> function EnumUserProc3: integer;<br> end;<br><br>function TTestObject.EnumUserProc3: integer;<br><br>var<br> TotalCount3: integer;<br><br> procedure CallBackHere3(Value: integer);<br>
begin<br> Inc(TotalCount3, Value);<br> end;<br><br>begin<br> TotalCount3:=0;<br> EnumValues(CallBackHere3, 10);<br> Result:=TotalCount3;<br>end;<br><br>begin<br> Writeln('Result 1 is ', EnumUserProc());<br>
Writeln('Result 2 is ', EnumUserProc2());<br> with TTestObject.Create do<br> begin<br> Writeln('Result 3 is ', EnumUserProc3());<br> Free;<br> end;<br>end.<br><br>