[fpc-devel] Closures via interfaces
DrDiettrich1 at aol.com
Mon May 27 16:17:35 CEST 2013
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.
ATM I have no opinion about this matter, because I don't plan to use
neither anonymous procedures nor functional programming myself. To me it
looks safer (better controllable) to use ordinary callback procedures
instead of anonymous ones, and for functional programming I'd only
accept an full-featured implementation, e.g. a LISP like sub-system
*written* in Pascal.
More information about the fpc-devel