[fpc-pascal] Functional Pascal

Marco van de Voort marcov at stack.nl
Thu Mar 1 12:41:12 CET 2012


In our previous episode, Lukasz Stafiniak said:
> >> (2) Closures. That is, making local functions that only use "const"
> >> arguments and "val / let" variables safe to return from the outer
> >> function. This can be done by allocating the "val / let" data on the
> >> heap, or perhaps easier by copying them into an implicitly built
> >> object and interpreting the returned local function as pointer to
> >> method of this object. The closure-object would be memory-managed as
> >> other objects.
> > Closures are already supported by Delphi 2009 and are currently being worked
> > on by someone in FPC.
> 
> Wow, I didn't know about that! Still, it is not clear to me it is a
> good thing to capture "var" variables in the closure. It goes against
> the semantics of normal nested functions. It's a "dirty but flexible"
> solution -- it makes it easy to define several closures out of a
> single definition (kind of like using partial application), e.g. when
> the anonymous function is inside a loop. Java doesn't have it
> (closures can only refer to "final" variables, "final"="val"="let").

Anonymous functions (the actual name of that feature), while closurelike,
but not really meant as a basis for functional programming I think.

They are mainly used in the framework to quickly/easily schedule some work
in a different thread.




More information about the fpc-pascal mailing list