[fpc-pascal] Feature Announcement: Function References and Anonymous Functions

Sven Barth pascaldragon at googlemail.com
Mon May 30 07:25:06 CEST 2022


Am 28.05.2022 um 14:04 schrieb Benito van der Zander via fpc-pascal:
> Hi,
>
>> Sort((left, right) begin
>>                      if left < right then
>>                        result := -1
>>                      else if left > right then
>>                        result := 1
>>                      else
>>                        result := 0;
>>                    end);
>
> One could introduce the @ operator to get a reference to a block of code.
>
> Sort( @(left, right) begin
>                      if left < right then
>                        result := -1
>                      else if left > right then
>                        result := 1
>                      else
>                        result := 0;
>                    end);

The sequence "@(identifier" is already a valid sequence (because 
"@(SomeVar)" is valid) and I'm definitely and absolutely against 
introducing another ambigous syntax as I already have enough headaches 
with Delphi's generic syntax.

>
> The "result" variable is also ugly. You could make "if" an expression:
>
> Sort( @(left, right) begin
>                      result :=
>                        if left < right then -1
>                        else if left > right then 1
>                        else 0;
>                    end);

Allowing if (and case) to be an expression, too, is something I'm still 
toying with independantly of this...

>
> Then the begin end could be removed too. Make @:= an operator to turn 
> an expression to a function
>
> Sort( @(left, right) :=
>                         if left < right then -1
>                         else if left > right then 1
>                         else 0;
>                    );
>

I personally would have a bit more verbose syntax though it would have 
the same problem regarding type inference which would be an absolute 
PITA to implement (not to mention the opposition from the other core devs):

=== code begin ===

Sort(lamba (left, right) as
   if left < right then -1
   else if left > right then 1
   else 0);

=== code end ===

Regards,
Sven
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20220530/da68286f/attachment.htm>


More information about the fpc-pascal mailing list