Confusion about dynamically and lexically scoped closures

Jakob Ovrum via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Nov 8 15:17:04 PST 2015


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.


More information about the Digitalmars-d-learn mailing list