Sven Barth wrote 11 lines
Am 28.02.2012 20:31, schrieb Lukasz Stafiniak:
>
> (1) A declaration part, that parallels "var", with keyword "val" or
> "let" (since "val" is taken up by a procedure). It introduces named
> values, i.e. non-assignable variables. The part after "=" can be any
> expression. Therefore, this feature spoils the "declaration --
> implementation divide" that is dear to Pascal.
I personally don't see a use for this... but feel free to provide a useful
example ;)
The main point is to perform some computation whose result is subsequently captured when the nested function is defined (becomes part of the closure).
Show quoted text 11 lines
> (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").
Best,
Łukasz