Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2010-11

Suggestion about ability to pass local procedures as variables

4 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Max Vlasov

Hi,

it appears recently several pieces if knowledge came to me, I understood how anonymous functions (aka closures) work :) and the fact that GNU Pascal allows passing local procedure as variables. I mentioned them together because as many I don't like the closures as they implemented everywhere including delphi (I read also the discussion here that was several month ago) and on the other side, I feel that allowing passing local procedures (if it's technically possible) is a better way to achieve the same. I mean, this will allow to catch local context for executing anything from external code (what's actually closures is about).

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

Thanks

Max Vlasov

Vincent Snijders
to Max Vlasov

2010/11/30 Max Vlasov :

Show quoted text 17 lines
Hi, it appears recently several pieces if knowledge came to me, I understood how anonymous functions (aka closures) work :) and the fact that GNU Pascal allows passing local procedure as variables. I mentioned them together because as many I don't like the closures as they implemented everywhere including delphi (I read also the discussion here that was several month ago) and on the other side, I feel that allowing passing local procedures (if it's technically possible) is a better way to achieve the same. I mean, this will allow to catch local context for executing anything from external code (what's actually closures is about). 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.

Vincent

Max Vlasov
to Vincent Snijders

On Tue, Nov 30, 2010 at 12:26 PM, Vincent Snijders <

Show quoted text 31 lines
wrote: 2010/11/30 Max Vlasov : > Hi, > > it appears recently several pieces if knowledge came to me, I understood how > anonymous functions (aka closures) work :) and the fact that GNU Pascal > allows passing local procedure as variables. I mentioned them together > because as many I don't like the closures as they implemented everywhere > including delphi (I read also the discussion here that was several month > ago) and on the other side, I feel that allowing passing local procedures > (if it's technically possible) is a better way to achieve the same. I mean, > this will allow to catch local context for executing anything from external > code (what's actually closures is about). > > 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. Vincent

Vincent, looks like this is it. I will try to set how it works. Thanks

Max

Max Vlasov
to Vincent Snijders: 2010/11/30 Max Vlasov : Nested procedural variables are a new feature in fpc 2.5.1. See:…

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.