Let's get the semantic around closure fixed.

Steven Schveighoffer schveiguy at gmail.com
Tue May 18 20:01:15 UTC 2021


On 5/18/21 3:32 PM, Ola Fosheim Grostad wrote:
> On Tuesday, 18 May 2021 at 19:20:55 UTC, Steven Schveighoffer wrote:
>> On 5/18/21 3:04 PM, Ola Fosheim Grostad wrote:
>> There's also the issue that if you have a scoped variable that has a 
>> destructor, the value will be destroyed (and probably unusable) if you 
>> call the delegate from outside the scope.
> 
> Ouch. Ok, so in OO languages like Simula, all scopes are heap-closures 
> and there is no stack, which kinda changes the game. I guess Javascript 
> does the same conceptually but the JIT perhaps extracts uncaptured 
> variables and puts those on a stack as an optimization? (My guess)
> 
> But how does a function with a delegate parameter know if it is safe to 
> store the delegate or not?

Shouldn't matter. The compiler should not compile code that allows you 
to use a dangling struct (i.e. a destroyed struct).

In fact, it used to be this way, but there was a "hack" introduced to 
allow it to compile.

See: https://issues.dlang.org/show_bug.cgi?id=15952

And the change that allowed it (clearly identified as a hack): 
https://github.com/dlang/dmd/pull/5292/files#diff-a0928b0b76375204c6f58973fb3f2748e9e614394d5f4b0d8fa3cb20eb5a96c9R757-R760

I don't pretend to understand most of this, it was other sleuths (mostly 
Paul Backus) that discovered this.

-Steve


More information about the Digitalmars-d mailing list