On 9/13/16 3:42 PM, Yuxuan Shui wrote:
On Tuesday, 13 September 2016 at 01:32:19 UTC, Steven Schveighoffer wrote:
On 9/12/16 4:11 PM, Ali Çehreli wrote:
On 09/10/2016 10:44 PM, Yuxuan Shui wrote:
I recently noticed nested struct capture its context by reference
(which, BTW, is not mentioned at all here:
https://dlang.org/spec/struct.html#nested).

" It has access to the context of its enclosing scope (via an added
hidden field)."

It needs to be a reference. Otherwise, you store the entire stack
frame in the struct? That wouldn't be a "field". It also has write
access to the context:

Why not just capture the variables that are actually been referenced?

There's nothing in the language to prevent this optimization.

Also being a field doesn't put limits on the size of the "field".

Again, could be clearer. But the fact that both the function and the struct affect the same data kind of dictates it needs to be a reference.

I like how C++ lambda lets you choose what variables to capture, and how
are they captured. I'm little disappointed that D doesn't let me do the
same.

Not familiar with C++ lambda. You can always "specify" how to capture the data by directly declaring it:

auto foo()
{
    int x;
    static struct S
    {
        int x;
    }
    return S(x);
}

In D, if you have a closure, it's going to be heap allocated. Just the way it is. If you don't want that, you have to avoid them.

-Steve

Reply via email to