[Issue 21745] Closure created in struct constructor passed to class constructor is not heap allocated

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed Mar 24 07:41:37 UTC 2021


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

anonymous4 <dfj1esp02 at sneakemail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
           Hardware|x86_64                      |All
         Resolution|---                         |INVALID
                 OS|Linux                       |All

--- Comment #6 from anonymous4 <dfj1esp02 at sneakemail.com> ---
(In reply to FeepingCreature from comment #4)
> Since it's a struct, it should save "this" by value though.

Closures don't capture variables by value, you would end up with two different
copies of the variable: one on the stack, another in the closure, but the
closure should share one instance of the variable. If you want two copies, do
it explicitly:

struct Foo
{
    int i;
    Bar bar;
    this(int i)
    {
        this.i = i;
        Foo f = this;
        this.bar = new Bar({ return f.i; });
    }
}

--


More information about the Digitalmars-d-bugs mailing list