[fpc-devel] Delphi anonymous methods

Michael Van Canneyt michael at freepascal.org
Mon Mar 4 13:23:41 CET 2013



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.

Michael.



More information about the fpc-devel mailing list