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

Benito van der Zander benito at benibela.de
Sat May 28 14:04:46 CEST 2022


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 "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);


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;
                   );





Benito
On 28.05.22 08:47, Hairy Pixels via fpc-pascal wrote:
> I’ve had some time to play with this now and my first piece of feedback is that given my experience with other languages, the most common usage of closures is by passing them as arguments to functions.
>
> Compared to the other languages I’m using now I’d say that we should be inferring more of the context of the receiving function type and not requiring the programmer to type out the full function header as in the example below (especially in the case there are no local variables declared).
>
> Sort(function(left, right: Double): integer
>                    begin
>                      if left < right then
>                        result := -1
>                      else if left > right then
>                        result := 1
>                      else
>                        result := 0;
>                    end);
>
> It’s hard to say what the best policy is for Pascal but some combination of the function/procedure keyword, parameter type names and return type could be omitted or shortened in various ways.
>
> Given we know the function type from the parameter list we could infer most of the information and provide a more limited set of syntax, for example like this (Swift like):
>
> Sort((left, right) begin
>                      if left < right then
>                        result := -1
>                      else if left > right then
>                        result := 1
>                      else
>                        result := 0;
>                    end);
>
> There is even the most limited shorthand (from Swift again) which uses token to represent the arguments as they are ordered.
>
> Sort(begin
>        if $0 < right then
>          result := -1
>        else if $0 > $1 then
>          result := 1
>        else
>          result := 0;
>      end);
>
> Regards,
> 	Ryan Joseph
>
> _______________________________________________
> fpc-pascal maillist  -fpc-pascal at lists.freepascal.org
> https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20220528/6a34e173/attachment.htm>


More information about the fpc-pascal mailing list