Let's get the semantic around closure fixed.

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


On 5/18/21 4:07 PM, Ola Fosheim Grostad wrote:
> On Tuesday, 18 May 2021 at 20:01:15 UTC, Steven Schveighoffer wrote:
>> I don't pretend to understand most of this, it was other sleuths 
>> (mostly Paul Backus) that discovered this.
> 
> I am not sure if my understanding of the language reference is correct, 
> but I get a feeling this is an area where one just have to try different 
> combinations and see what happens.
> 
> 

No, it was correct before the hack. Code which captured a struct that 
would be destroyed outside the scope just wouldn't compile. Now it does.

An example:

struct S
{
    bool destroyed = false;
    ~this() { destroyed = true; }
}

void main()
{
    void delegate() dg;
    {
      S s;
      dg = {writeln("destroyed = ", s.destroyed);};
      dg(); // destroyed = false
    }
    dg(); // destroyed = true
}

So basically, depending on when you call the delegate, the thing could 
be invalid. Not a big deal (maybe?) for a boolean, but could cause real 
problems for other things. And the user expectation is that when you 
capture the variable, it's how it was when you captured it. At least it 
should live as long as the delegate is alive, no?

-Steve


More information about the Digitalmars-d mailing list