[fpc-devel] Delphi anonymous methods

Michael Van Canneyt michael at freepascal.org
Mon Mar 4 14:05:02 CET 2013



On Mon, 4 Mar 2013, Sven Barth wrote:

> Am 04.03.2013 13:23, schrieb Michael Van Canneyt:
>> 
>> 
>> On Mon, 4 Mar 2013, Mattias Gaertner wrote:
>> 
>>> On Mon, 04 Mar 2013 11:17:53 +0000
>>> Martin <lazarus at mfriebe.de> wrote:
>>> 
>>>> On 04/03/2013 04:54, Boian Mitov wrote:
>>>>> Here is example:
>>>>> 
>>>>> Parallel execution with selection of executor:
>>>>> 
>>>>> for i := 0 to AMaxScaleIndex - 1 do
>>>>>  begin
>>>>>  APerIterationLocations.Add( TRTDynamicList<TVLImageRect>.Create() );
>>>>>  AExecutionTask.Add( AExecutor.Execute(
>>>>>      procedure()
>>>>>      begin
>>>>>        ProcessOne( levelScale[ i ], img, hitThreshold, winStride,
>>>>> padding, APerIterationLocations[ i ] );
>>>>>      end
>>>>>    ));
>>>>>
>>>>>  end;
>>>>> 
>>>>> In this case, not only you save declaration, you save the need to
>>>>> write a whole new class just for the task.
>>>>> This code reduced well over 30 lines of code alone.
>>>>> I have a lot of other examples as well ;-) .
>>>> 
>>>> Ok, I can see the closure helping, but why the anonymous procedure?
>>>> 
>>>> If FPC would offer something like this:
>>>> 
>>>> Procedure Foo;
>>>> var
>>>>    // your locals
>>>>       procedure  Bar(); closure;
>>>>         begin
>>>>           ProcessOne( levelScale[ i ], img, hitThreshold, winStride,
>>>> padding, APerIterationLocations[ i ] );
>>>>         end
>>>> begin
>>>>    // your code from above
>>>>    for i := 0 to AMaxScaleIndex - 1 do
>>>>     begin
>>>>     APerIterationLocations.Add( TRTDynamicList<TVLImageRect>.Create() );
>>>>     AExecutionTask.Add( AExecutor.Execute( @Bar  ));
>>>> end;
>>>> 
>>>> I added:
>>>>   - the name "Bar"
>>>> - Used is at reference
>>>> - the keyword "closure"
>>>> 
>>>> The above code would then create the exact same closure, as your code
>>>> does. And it does not need an anonymous method.
>>> 
>>> Nice. Gimme, gimme.
>> 
>> IMHO this is a pefectly acceptable solution. Adds only a modifier 
>> (modifiers are not keywords) and offers all advantages of closures.
>> 
>> No need for 'anonymous' procedures, which are very unpascal-ish.
>> People who want that should not be using Pascal to begin with.
>
> If you guys would read what I wrote in one of my first answers to Vasiliy in 
> this thread than you would know that I want him to implement this possiblity 
> as well... but after all, who reads the mails of some strange computer 
> science student who just happens to work on the compiler -.-
>
> To rephrase: implement support for anonymous functions for Delphi 
> compatibility (maybe coupled to a mode switch) and allow for all other modes 
> the assignment of nested functions/procedures to "reference to" procvars 
> instead. Additionally a shortened "lambda syntax" can be added for really 
> simple cases.

My apologies, I didn't read the whole thread.

But yes, your proposal seems OK.

And the first to use anonymous functions in FPC distributed code, 
I will personally make him eat his keyboard. Without pepper and salt.

OK. I might hire "Inkasso-Team Moskou" to help with this ;-)

Michael.



More information about the fpc-devel mailing list