[fpc-devel] Delphi anonymous methods
Martin
lazarus at mfriebe.de
Mon Mar 4 23:56:40 CET 2013
On 04/03/2013 22:16, Sven Barth wrote:
>
> > The above is NOT an anonymous function. It is a reference to a term.
> > I am not voting for it, but IF it was done, then it should have its
> own keyword.
> >
> > It would not allow for var, type or anything (and it will only work
> for functions, not procedures.)
> > a.sort(LambdaTerm TMyCompare do x > y)
> > or
> > a.sort(TermRef TMyCompare(x > y) )
>
> This is basically what my idea behind the lambs syntax was: only one
> expression or statement and no variable, type or constant
> declarations. And for more complex use case there are either Delphi's
> anonymous functions or FPC's nested procedures/functions.
>
Ok, so then it is important, that they are distinguished. Starting with
their own name. We MUST NOT call them anonymous functions. (despite the
do construct a function. But they are not, because the skip the
Result:= part and other)
Technically they are a subset:
1- like a function, they have a result
2- like a function the have zero, 1 or more params
3- not like a functions, they can not have var or out param
Well you can construct cases were they are possible: (* Result := *)
1 + Foo(VarParam);
But I think they should not be allowed
4- There body must always contain a RHS term/expression
This also results in that the can only be passed for functions
5- begin end is not allowed (would not be an RHS
About 4) If you want procedures to, then we can not use an RHS, because
a procedure needs a full statement in the body.
That would mean functions would have to include the "Result :=". There
is no way of smart detection, because a function can also sot the result
by doing "CallFoo(Result)" (CallFoo has an out param)
It is either or, but no mixture.
Even if it goes for proceddure style, 5 should still be considered.
> > anonymous function, then would *always* require
> > a. sort( Function; prototype TMyCompare; [var ...] begin ... end )
> >
> > the protopybe (modifier) could work for named procedures too. (THey
> would be required NOT to have any params)
> > Function Foo; prototype TMyCompare;
> > [var ...]
> > begin ... end
>
> Ah! Now with the named example I understand the use of the ";" after
> the "function"...
>
I think using a modifier is the best way to specify the parameter template.
More information about the fpc-devel
mailing list