[Issue 4419] __gshared static in class has no effect

d-bugmail at puremagic.com d-bugmail at puremagic.com
Fri Sep 13 14:01:22 PDT 2013


http://d.puremagic.com/issues/show_bug.cgi?id=4419



--- Comment #11 from Andrej Mitrovic <andrej.mitrovich at gmail.com> 2013-09-13 14:01:21 PDT ---
(In reply to comment #10)
> (In reply to comment #9)
> 
> > I can't reproduce this, can you paste what 'chan' is, or just paste the full
> > example? E.g.:
> > 
> 
> Could you try this?
> class B { bool v;}
> 
> class C
> {
>     shared B ready = new shared B;
>     static shared B statReady = new shared B;
> 
> }
> 
> void main()
> {
>     auto a = new C;
>     auto b = new C;
>     a.ready.v = true;
>     assert(b.ready.v == false);
>     assert(&a.ready.v !is &b.ready.v);
> 
>     a.statReady.v = true;
>     assert(b.statReady.v == true);
>     assert(&a.statReady.v is &b.statReady.v);
> }

This is a slightly different issue, it's the field initializer that is tricking
you into thinking it's a new instance per class, but in fact that initializer
is invoked at *compile-time*. Try this:

-----
class B
{
    bool v;
}

class C
{
    this() { ready = new shared B; }

    shared B ready;
    static shared B statReady = new shared B;
}

void main()
{
    auto a = new C;
    auto b = new C;
    a.ready.v = true;
    assert(b.ready.v == false);
    assert(&a.ready.v !is &b.ready.v);

    a.statReady.v = true;
    assert(b.statReady.v == true);
    assert(&a.statReady.v is &b.statReady.v);
}
-----

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list