D 2015/2016 Vision?

Meta via Digitalmars-d digitalmars-d at puremagic.com
Mon Oct 5 12:07:19 PDT 2015


On Monday, 5 October 2015 at 17:19:09 UTC, Gary Willoughby wrote:
> This can be shortened to:
>
> import std.stdio;
> import std.typecons;
>
> class A
> {
> 	string name;
>
> 	this(string name)
> 	{
> 		this.name = name;
> 	}
>
> 	void hello()
> 	{
> 		writeln("Hello, ", this.name);
> 	}
> }
>
> void main()
> {
> 	auto a = scoped!A("Foo");
> 	a.hello();
> }

There's a critical flaw in `scoped`. Observe:

import std.stdio;
import std.typecons;

class A
{
	string name;

	this(string name)
	{
		this.name = name;
		writeln("Creating A");
	}

	~this()
	{
		writeln("Destroying A");
	}

	void hello()
	{
		writeln("Hello, ", this.name);
	}
}

void main()
{
	auto a1 = scoped!A("Foo");
	a1.hello();

	A a2 = scoped!A("Foo");
	a2.hello();
}


The output:

Creating A
Hello, Foo
Creating A
Destroying A
Destroying A
object.Error: Access Violation


More information about the Digitalmars-d mailing list