I was thinking about how Rust can return arbitrarily from blocks. It occurred to me recently that there's no reason you can't do that in D. I'm just not sure if there are any limitations. For instance, in the code below, I create an object but don't allocate anything, then in a block I create a garbage collected variable and assign it to it. Everything seems to work fine. I'm just not sure if there are any gotchas to be aware of.

class Foo
{
        int baz = 2;
}

void main()
{
        import std.stdio : writeln;
        
        Foo foo;
        {
                Foo bar = new Foo();
                foo = bar;
        }
        //bar is now out of scope
        assert(foo.baz == 2);
}

Reply via email to