[fpc-devel] Closures via interfaces

Sven Barth pascaldragon at googlemail.com
Mon May 27 17:29:42 CEST 2013

Am 27.05.2013 16:18 schrieb "Hans-Peter Diettrich" <DrDiettrich1 at aol.com>:
> Vasiliy Kevroletin schrieb:
>> I described details of proposed and partly implemented approach in
mantis. This is Delphi's approach. Some people call it "closures via
interfaces". In slightly modified way it will allow to capture variables by
value (which was requested in previous discussion). I like this approach
because it's simple: it doesn't require to change codegenerator or rtl. On
the other hand for each closure it creates new interface. And almost for
each closure it creates new class. As a result: space overhead because of
virtual tables and rtti.
>> My question to community is: what do you think about proposed
implementation? Are there arguments for another approach?
> In contrast to anonymous procedures, true closures require fixed and
> immutable parameters. This requirement will disallow also references to
> variables outside the function, e.g. in their enclosing procedure. This
> would require deep(!) copies of all values/variables used in the
> anonymous procedure body, together with tests of already copied memory
> objects. How to create a deep copy at all, of e.g. an interface
> reference or a Handle?
> IMO we have to decide in the first place whether to implement anonymous
> procedures for Delphi compatibility *only*, or whether we want true
> closures (functional programming) inside the imperative OPL.

The values of local variables are captured for each closure. While in
Delphi they are captured by reference (basically; it's in fact a bit more
complicated) Vasiliy plans to also allow capturing by value. Inside the
procedure body itself the usual rules apply, so there won't be any
immutability. Global variables will behave like in normal procedures and
also the state of object oriented variables can be modified. So it's mostly
consistent with the remaining way that Pascal's procedures work with a few
additional concepts that heed to be documented accordingly.

Also there is no "true" way of closures, as in the end each language does
have its own behavior.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-devel/attachments/20130527/33129839/attachment.html>

More information about the fpc-devel mailing list