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@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
_______________________________________________
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Reply via email to