Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2012-03

Functional Pascal

3 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Lukasz Stafiniak
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

Reply by email

Marco van de Voort
to Lukasz Stafiniak

In our previous episode, Lukasz Stafiniak said:

Show quoted text 17 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").

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.

Reply by email

Lukasz Stafiniak
to Lukasz Stafiniak: On Wed, Feb 29, 2012 at 11:31 AM, Sven Barth The main point is to perform some…
Lukasz Stafiniak wrote 8 lines
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").

I'm sorry, I got confused -- the closure in Pascal does not capture the value of the variable (i.e. does not form a copy of the variable), rather, the variable becomes heap-allocated (implicitly a field of some class), and anonymous functions together with the function that contains them share the variable. Making a copy would be an interesting design choice... (although violating the semantics of nested procedures.)

http://www.reddit.com/r/programming/comments/6tp3i/pascal_gets_closures_before_java_why_hasnt_the/

Reply by email