[fpc-devel] Delphi anonymous methods
Mattias Gaertner
nc-gaertnma at netcologne.de
Mon Mar 4 13:19:23 CET 2013
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.
Mattias
More information about the fpc-devel
mailing list