[fpc-pascal] Re: Delphi's anonymous functions in Free Pascal
Michael Van Canneyt
michael at freepascal.org
Sun Nov 6 01:30:15 CET 2011
On Sat, 5 Nov 2011, Bernd wrote:
>
> A closure has enclosed variables from the surrounding scope where it
> was created, hence the name 'closure'. You cannot do this with a
> procedure variable alone, you need an object on the heap to contain
> the enclosed variables. The Delphi compiler will behind the scenes
> create a reference counted object with an 'invoke' method (the
> procedure SomeInner) and all its needed variables.
>
> It is basically a syntactic shortcut to explicitly defining this
> class, and creating an instance of it.
I understand all that. That's not the issue.
I didn't mean to say that my example will work with the compiler NOW.
Obviously, it would need some extension of the compiler to implement closures.
The only thing I am arguing is that the syntax could have been more clear.
Definining a function inside code (or as an argument to another function)
is just not Pascal; It goes against everything Pascal stands for.
You could perfectly explicitly declare the closure function outside the code block
it is used in, as long as it is AFTER the variables it would need from the
outer scope. That is what I wanted to show with my snippet of code. Probably
you'd need some kind of extra keyword to distinguish it from 'normal' nested functions.
I am not against closure functionality, although I highly doubt it is *that*
useful as some people make it out to be. Even so, I use it in my Javascript
programming.
But in Pascal, I would like to see it implemented in a way that doesn't go
against Pascal philosophy. In that regard, I consider the way that
Borland/CodeGear/Embarcadero implemented it a monstrosity.
It simply hurts the eyes...
Michael.
More information about the fpc-pascal
mailing list