On Tue, Nov 30, 2010 at 12:26 PM, Vincent Snijders <
Show quoted text 15 lines
wrote:
2010/11/30 Max Vlasov :
> So, I suppose that implementing closures is not in real plans (it's ok, I
> can live with that), but what about the second, is it possible?
>
> I will glad to hear different opinions from the developers
>
Nested procedural variables are a new feature in fpc 2.5.1. See:
http://svn.freepascal.org/cgi-bin/viewvc.cgi?view=rev&revision=15656
I don't know if that is what you are asking for.
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.
Max
program test;
{$mode delphi}
{$modeswitch nestedprocvars}
type
TCallBackProc = procedure(Value: integer) is nested;
procedure EnumValues(const Proc: TCallBackProc; Count: integer);
begin
while Count >= 0 do
begin
Dec(Count);
Proc(Count)
end;
end;
{ ************** Callback is a local procedure inside another procedure *************** }
function EnumUserProc: integer;
var
TotalCount: integer;
procedure CallBackHere(Value: integer);
begin
Inc(TotalCount, Value);
end;
begin
TotalCount:=0;
EnumValues(CallBackHere, 10);
Result:=TotalCount;
end;
{ **************** Callback is a procedure (classic) ************* }
var
TotalCount2: integer;
procedure CallBackHere2(Value: integer);
begin
Inc(TotalCount2, Value);
end;
function EnumUserProc2: integer;
begin
TotalCount2:=0;
EnumValues(CallBackHere2, 10);
Result:=TotalCount2;
end;
{ ************* Callback is a local procedure inside a method **************** }
type
TTestObject = class(TObject)
function EnumUserProc3: integer;
end;
function TTestObject.EnumUserProc3: integer;
var
TotalCount3: integer;
procedure CallBackHere3(Value: integer);
begin
Inc(TotalCount3, Value);
end;
begin
TotalCount3:=0;
EnumValues(CallBackHere3, 10);
Result:=TotalCount3;
end;
begin
Writeln('Result 1 is ', EnumUserProc());
Writeln('Result 2 is ', EnumUserProc2());
with TTestObject.Create do
begin
Writeln('Result 3 is ', EnumUserProc3());
Free;
end;
end.