[fpc-devel] Delphi anonymous methods
Alexander Klenin
klenin at gmail.com
Mon Mar 4 17:05:21 CET 2013
On Tue, Mar 5, 2013 at 2:10 AM, Martin <lazarus at mfriebe.de> wrote:
>
> First: Stressing out: I don't like it. But if we must have one, the lambda
> approach is the best one yet.
> Reason: At least the type is declared at a pascal-like location.
I certainly agree that it is subjective in the sense that some persons
do like it, and other do not.
However my argument is that is an essential language construct, similar to
"for" loop, for example.
It is certainly possible to dislike "for" loops, and always use "while" instead.
There is even a programming language which explicitly lacks "for"
based on that reasoning.
Nevertheless, most programmers use "for" loops, because the value of
"syntax sugar" added by it
is enough to out-weight the burden of learning and understanding one
more language construct.
Anonymous functions (with good syntax, of course) fall in this category.
The world recognized that fact -- rather slowly, to be sure, but
remember that "while"s and "for"s
also took decades to be accepted as standard constructs.
> I did not note at first, but the al leaves the "Result" away. So X+5 now
> looks like a statement.
> What would happen with
> ATree.VisitPreorder(lambda TVisitor as begin X := min(10,x); X + 5; end);
> Reminds me of perl: the last value is the return value.
No, my proposal (which was made in the previous thread in this topic)
is following (all-caps denotes non-terminal):
1.1) A function of the form
function NAME(PARAMS): RETURNTYPE; begin Result := EXPRESSION; end;
can be shortened to
function NAME(PARAMS): RETURNTYPE as EXPRESSION
1.2) A procedure of the form
procedure NAME(PARAMS); begin STATEMENT; end;
can be shortened to
procedure NAME(PARAMS) as STATEMENT
1.3) All other cases (i.e. with several statements in the body) must
be spelled in full.
2.1) Given types
TFuncType = function (PARAMS): RETURNTYPE;
TProcType = procedure (PARAMS);
2.2) the function declaration
function NAME(PARAMS): RETURNTYPE;
can be shortened to
lambda NAME: TFuncType;
2.3) the anonymous function declaration
function (PARAMS): RETURNTYPE;
can be shortened to
lambda TFuncType;
2.4) Similarly, for procedures:
lambda NAME: TProcType;
lambda TProcType;
So, the answer to your question will be:
ATree.VisitPreorder(lambda TVisitor; begin
X := min(10,x);
Result := X + 5;
end);
Note that both proposals are totally orthogonal both to each other and
to anonymous functions,
and are useful in isolation -- in particular, "lambda" proposal helps
with event handlers:
1) It explicitly declares that a given procedure is supposed to be
assigned to an event of certain type,
thus increasing readability.
2) It shortens code, especially for a case of many parameters.
3) It allows to add new parameters to event handlers without breaking user code,
thus reducing the likehood of ugly solutions like "OnDrawCellEx".
Both "lambda" and "as" keywords are quite debatable, of course.
> If you look for the most natural, pascal like (ignoring the part that
> declaring a function inline, IMHO is not that)
>
> ATree.VisitPreorder(TVisitor(Result := X + 5);
> ATree.VisitPreorder(TVisitor(begin Result := X + 5; end);
>
> You are type casting the code, into a procedure of the given type.
There is certainly a merit in that, but it violates another Pascal
"basic principle" -- "prefer keywords to symbols".
Also, note that you can not omit Result assignment,
since ATree.VisitPreorder(TVisitor(X + 5)); is syntactically ambiguous.
So it becomes
ATree.VisitPreorder(TVisitor(Result := X + 5));
vs
ATree.VisitPreorder(lambda TVisitor as X + 5);
Finally, I disagree with unbalanced parenthesis -- but that's probably a typo?
--
Alexander S. Klenin
More information about the fpc-devel
mailing list