[fpc-devel] Question about Syntax: I there a reason for this design?

Jonas Maebe jonas.maebe at elis.ugent.be
Sun Sep 21 19:23:51 CEST 2014

[Jonas: anonymous methods and local functions could use the same syntax]

On 21/09/14 18:43, Boian Mitov wrote:
> I used to think the same way, until I actually started to use anonymous
> methods, and I realized what a full I used to be.
> The simplest difference is that you can return anonymous method as a
> result of your function and it will preserve all the captured context.
> Local methods can't do that.

I'm talking about syntax, not behaviour. When assigning a local function
to an anonymous method ("reference to ..."), the context will have to be
captured in exactly the same way as when specifying an inline nested
function (aka anonymous method) that requires context. A nested function
and a "reference to..." are different types, so a type conversion would
have to be inserted by the compiler anyway (a bit similar to how
assigning a method to a "procedure of object" works, which also captures
context, be it in a much simpler way).

You could still first keep the context on the stack for quick access by
the parent function and later add indirection if necessary (in case it
can escape the life of the parent function), as handled by e.g. C blocks.

> Furthermore local methods will not work when passed as parameter to
> another thread, as they lose context when the owner function exits.

It's the same thing.


More information about the fpc-devel mailing list