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

Reply via email to