[fpc-devel] Delphi anonymous methods

Sven Barth pascaldragon at googlemail.com
Sat Mar 2 20:55:20 CET 2013


On 02.03.2013 20:03, vrt277 wrote:
> Implementation of anonymous methods was started in the branch. Author
> didn't submit anything last year and forgot commit one added file. But
> changes from branch can be useful, AFAIU parsing of anonymous functions
> without capturing of local variables was implemented.
>
> Does someone know is author available? Does he plan to continue work, or
> I can work on implementation without collaboration with him ? Or at
> least can he send missed file ?
>

I have already written the author some time ago. He has said that he 
plans to at least commit the missing files. If you would continue his 
work (in a new branch please with a complete copy of trunk...) then he 
might be relieved as well.

> It's hard to properly split this complex task into subtasks. I can
> propose 2 steps:
>    1. Parsing and generation of anonymous function. Capturing of
>    variables is forbidden.
>    2. Capturing of variables is implemented.
>
> A question for fpc team: do you agree that support of Delphi-compatible
> anonymous methods should be implemented?

At least I do, yes.

> Also there are open questions which require brainstorm:
> 1. Does Pascal needs other implementation of closures which is different
> from anonymous methods implementation?

I would say no. After all the method implementation itself stays the 
same, but the captured variables should be different. Or what does this 
print:

=== example begin ===

var
   arr: array[0..5] of reference to procedure;
   i: LongInt;
begin
   for i := Low(arr) to High(arr) do
     arr[i] := procedure
            begin
              Writeln(i);
            end;

   Writeln('Output: '); // just to ensure that the optimizer doesn't 
combine the two loops

   for i := Low(arr) to High(arr) do
     arr[i]();
end;

=== example end ===

> 2. Does Pascal needs short syntax for closures?

I would say yes, but this can be done in a seperate step

> I if someone want to discuss these questions it will be better to create
> in separate threads.

Two personal things I'd like to note as I have already planned to 
continue working on anonymous functions myself:
- add support to pass nested functions to "reference to" procvars; this 
will allow users who don't want to use the anonymous syntax to use the 
"closure" feature as well; please note that this must be done in a way 
that passing nested functions to "is nested" procvars is not broken
- (in context with the above point) couple the support for the anonymous 
functions to a new modeswitch (enabled by default in mode delphi), but 
allow "reference to" procvars in non-Delphi modes (this way you'd only 
be able to use nested functions, but later on the shorter syntax should 
be used there as well)

Regards,
Sven




More information about the fpc-devel mailing list