[fpc-devel] Delphi anonymous methods

vrt277 vrt277 at gmail.com
Sat Mar 2 20:03:33 CET 2013


Hi,

I want to implement support of Delphi anonymous methods for fpc. As I 
know I should create good proposal. Fortunately Delphi already made half 
of my work.
Proposal: all behaviour described in 
http://docwiki.embarcadero.com/RADStudio/XE3/en/Anonymous_Methods_in_Delphi 
should be implemented.

Anonymous methods are like closures in other languages. Main features of 
anonymous methods and closures are
- access to local variables
- extend lifetime of used local variables.

Difference between anonymous methods in Delphi and closures in most 
other languages is that you can not create many instances of same 
anonymous methods during work of it's declaring function. I don't know 
how to say better, but example can help:

=begin example

type TProc = reference to Procedure;
var i: Integer;
     arr: array[1..5] of TProc;
begin
   for i := 1 to 5 do
     arr[i] :=
       procedure begin
       end;
   for i := 1 to 4 do
     Write(arr[i] = arr[i+1], ' ');
end.

=end example

Someone who thinks that first cycle creates different anonymous methods 
is wrong. All values in arr will be equal after cycle execution. Here 
more complicated example of same feature:
https://gist.github.com/vkevroletin/5069653

I investigated behaviour and implementation(slightly) of anonymous 
methods in Delphi. Results are in document below. It have little 
introduction which is not very useful for advanced developers but may be 
helpful in wiki to describe some aspects of compiler. Both links point 
to same document in different formats.
plain text: 
https://raw.github.com/vkevroletin/Diploma/master/anonym_method_delphi.org
pdf: 
https://github.com/vkevroletin/Diploma/blob/master/anonym_method_delphi.pdf

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 ?

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?

Also there are open questions which require brainstorm:
1. Does Pascal needs other implementation of closures which is different 
from anonymous methods implementation?
2. Does Pascal needs short syntax for closures?
I if someone want to discuss these questions it will be better to create 
in separate threads.

Link to closures branch: 
http://svn.freepascal.org/cgi-bin/viewvc.cgi/branches/blaise/closures/

Vasiliy K.




More information about the fpc-devel mailing list