Gotchas for returning values from blocks

jmh530 via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Jun 12 11:24:58 PDT 2016


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);
}


More information about the Digitalmars-d-learn mailing list