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

Hairy Pixels genericptr at gmail.com
Sat May 28 08:47:37 CEST 2022


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



More information about the fpc-pascal mailing list