[fpc-pascal] [fpc-devel] Nested function closures

Graeme Geldenhuys mailinglists at geldenhuys.co.uk
Wed Apr 28 00:53:27 CEST 2021


On 27/04/2021 10:13 pm, Ryan Joseph via fpc-pascal wrote:
>      value.SortEntities(function(a, b: TEntity): integer
>        begin
>          // do stuff
>        end
>      );

It seem the beginning of the thread is missing, but I would like to
comment on something here - purely based on the example code given.

The SortEntities() is defined with a signature of type TComparator,
which in turn is a function that takes 2 arguments of type TEntity
and returns a integer.

Why must the anonymous function repeat all that information again,
as shown in the quoted code above? Can't the compiler figure all
that out simply by inference? All the developer should have to do
is give the 2 parameters a name, and a function body (block of
code). Thus you could end up with something like this:


     value.SortEntities((a, b) ->
       begin
         // do stuff with a and b, then
         // return some integer.
       end
     );


Yes, I stole the "->" symbol from another language simply to separate
the parameters from the function body. Replace that with any
Pascal-like symbol.


Regards,
  Graeme


More information about the fpc-pascal mailing list