<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body smarttemplateinserted="true">
<div id="smartTemplate4-template">Hi, <br>
</div>
<div><br>
</div>
<div>
<blockquote type="cite">
<pre class="moz-quote-pre" wrap="">Sort((left, right) begin
if left < right then
result := -1
else if left > right then
result := 1
else
result := 0;
end);
</pre>
</blockquote>
<br>
<div>One could introduce the @ operator to get a reference to a
block of code.<br>
</div>
<div>
<p> </p>
<blockquote type="cite"> </blockquote>
<pre class="moz-quote-pre" wrap="">Sort( @(left, right) begin
if left < right then
result := -1
else if left > right then
result := 1
else
result := 0;
end);
</pre>
</div>
</div>
<div><br>
</div>
<div>The "result" variable is also ugly. You could make "if" an
expression:<br>
</div>
<div><br>
</div>
<div>
<pre class="moz-quote-pre" wrap="">Sort( @(left, right) begin
result :=
if left < right then -1
else if left > right then 1
else 0;
end);
</pre>
<div><br>
</div>
<p> </p>
Then the begin end could be removed too. Make @:= an operator to
turn an expression to a function</div>
<br>
<div>
<pre class="moz-quote-pre" wrap="">Sort( @(left, right) :=
if left < right then -1
else if left > right then 1
else 0;
);
</pre>
<div><br>
</div>
<p> </p>
<br>
<div><br>
</div>
</div>
<div><br>
Benito </div>
<div class="moz-cite-prefix">On 28.05.22 08:47, Hairy Pixels via
fpc-pascal wrote:<br>
</div>
<blockquote type="cite"
cite="mid:BF52BBB6-9C9A-4ED5-9E52-D590A8F575F8@gmail.com">
<pre class="moz-quote-pre" wrap="">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 - <a class="moz-txt-link-abbreviated moz-txt-link-freetext" href="mailto:fpc-pascal@lists.freepascal.org">fpc-pascal@lists.freepascal.org</a>
<a class="moz-txt-link-freetext" href="https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal">https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal</a>
</pre>
</blockquote>
</body>
</html>