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

Hairy Pixels genericptr at gmail.com
Sat May 28 16:09:25 CEST 2022



> On May 28, 2022, at 7:04 PM, Benito van der Zander via fpc-pascal <fpc-pascal at lists.freepascal.org> wrote:
> 
> 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);

Swift, C# and JavaScript etc…  allow you to omit the return statement if the function body is a single expression. I don’t think any languages I’m aware of break the rules of expression though like this is proposing but you could use exit() and it would look a little better.

> 
> 
> 
> 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 don’t think you need the @ operator here since the ():= is enough to denote a function. This is like the “arrow” syntax in JavaScript and it makes sense to me. It’s a good idea I think.

Come to think of it is FPC going to support capture lists? Right now the entire scope is captured but this is not always ideal. Right now there’s no syntax currently available that would support that.


Regards,
	Ryan Joseph



More information about the fpc-pascal mailing list