www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Confusion about dynamically and lexically scoped closures

reply ParticlePeter <ParticlePeter gmx.de> writes:
Hi,
the confusion starts here: http://dlang.org/function.html#closures
End of paragraph bellow the last delegate example:
"This combining of the environment and the function is called a 
dynamic closure."

While according to 
https://en.wikipedia.org/wiki/Scope_%28computer_science%29
"Lexical scope vs. dynamic scope" I would call D delegates 
lexical closures, as the scope of the callee (scope where callee 
is defined) is used for free variable lookup, isn't that right?

Confusion is enhanced with this Dlang wiki: 
http://wiki.dlang.org/Function_literals
"This has brought up the specter of Dynamic Closures. The nested 
and/or anonymous functions can only access dynamic scope, which 
means scope that exists on the stack at the time of execution. 
This differs from lexical scope, which refers to the scope as 
indicated by the program text structure."

So what's what now?

Cheers, ParticlePeter
Nov 08 2015
parent reply Jakob Ovrum <jakobovrum gmail.com> writes:
On Sunday, 8 November 2015 at 23:08:05 UTC, ParticlePeter wrote:
 Hi,
 the confusion starts here: 
 http://dlang.org/function.html#closures
 End of paragraph bellow the last delegate example:
 "This combining of the environment and the function is called a 
 dynamic closure."

 While according to 
 https://en.wikipedia.org/wiki/Scope_%28computer_science%29
 "Lexical scope vs. dynamic scope" I would call D delegates 
 lexical closures, as the scope of the callee (scope where 
 callee is defined) is used for free variable lookup, isn't that 
 right?

 Confusion is enhanced with this Dlang wiki: 
 http://wiki.dlang.org/Function_literals
 "This has brought up the specter of Dynamic Closures. The 
 nested and/or anonymous functions can only access dynamic 
 scope, which means scope that exists on the stack at the time 
 of execution. This differs from lexical scope, which refers to 
 the scope as indicated by the program text structure."

 So what's what now?

 Cheers, ParticlePeter
The closures for delegates in D1 are never automatically copied to the heap, while in D2 this is done when it's determined that the delegate might outlive one of its upvalues. So, I think it's safe to say we have lexical closures in D2 but only dynamic closures in D1 and the language specification is out of date.
Nov 08 2015
parent ParticlePeter <ParticlePeter gmx.de> writes:
On Sunday, 8 November 2015 at 23:17:06 UTC, Jakob Ovrum wrote:
 The closures for delegates in D1 are never automatically copied 
 to the heap, while in D2 this is done when it's determined that 
 the delegate might outlive one of its upvalues.

 So, I think it's safe to say we have lexical closures in D2 but 
 only dynamic closures in D1 and the language specification is 
 out of date.
Thanks, makes sens, I assumed a typo in the docs.
Nov 08 2015