[Issue 23375] enum is not considered global mutable state

d-bugmail at puremagic.com d-bugmail at puremagic.com
Mon Sep 26 12:28:42 UTC 2022


https://issues.dlang.org/show_bug.cgi?id=23375

--- Comment #2 from Bolpat <qs.il.paperinik at gmail.com> ---
As enum was intended to be a replacement of C’s `#define` constants, the
compiler should thoroughly treat `enum` as a named literal; it can support
“mutable” indirections, but a lot more has to be taken. By “mutable” I mean
typed non-const, not that actual mutation is valid.

An easy way would be to specify that enums are deep-copied at usage site. This
might be possible because circular definitions are rejected:

    class C
    {
        C bestie;
        this() { }
        this(C friend) { bestie = friend; }
    }

    enum C me = new C(you);
    enum C you = new C(me); // error
    enum C[] us = [ me, you ];

But you can cheese it:

    enum C[] us = [ new C, new C ];

    shared static this()
    {
        us[0].bestie = us[1];
        us[1].bestie = us[0];
    }

    void main()
    {
        assert(us[0].bestie is us[1]);
        assert(us[1].bestie is us[0]);
        assert(us[0].bestie.bestie is us[0]);
        assert(us[1].bestie.bestie is us[1]);
    }

--


More information about the Digitalmars-d-bugs mailing list