On 07/27/2016 09:19 PM, Dechcaudron wrote:
struct Foo
{
[...]

    void ping() shared
    {
[...]
    }

    void fire()
    {
        spawn(&explode);
    }

    void explode() shared
    {
        ping();
    }
}

void main()
{
    auto a = Foo(1, 2);
    a.fire();

    thread_joinAll();
}

[...]

Is there anything I'm doing wrong? I won't lie, data sharing is the only
thing about D I don't find quite usable yet. Can anybody help me out on
this?

I think the program should not compile. You can't call a shared method on an unshared struct/class, so you shouldn't be able to take make a delegate of it and call that.

Reduced code:

----
struct Foo
{
    void ping() shared {}
}

void main()
{
    Foo a;
    // a.ping(); // rejected
    (&a.ping)(); // accepted
}
----

We can also break immutable/const with this:

----
struct Foo
{
    int x = 0;
    void ping() { x = 1; }
}

void main()
{
    immutable Foo a;
    // a.ping(); // rejected
    (&a.ping)(); // accepted
    assert(a.x == 0); // fails
}
----

Looks pretty bad. There's an open issue on this: https://issues.dlang.org/show_bug.cgi?id=16095

Reply via email to