[Issue 18945] immutable variable is used as if it's an enum

d-bugmail at puremagic.com d-bugmail at puremagic.com
Tue Jun 5 12:27:08 UTC 2018


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

--- Comment #3 from Jonathan M Davis <issues.dlang at jmdavisProg.com> ---
This probably does need to be removed via deprecation rather than simply making
it an error, but allowing it is exactly the sort of thing that increases the
confusion about how CTFE works and when it's used. Currently,

    immutable i = foo();

doesn't do CTFE. But

    immutable i = foo();
    int[i] arr;

does, and that muddies the waters considerably. We already have problems due to
folks getting really confused about why stuff like

    auto foo(int i)
    {
        return bar!i();
    }

isn't legal, and allowing a runtime variable to be used just because it's
immutable makes that worse - even more so when you consider that

    auto foo(immutable int i)
    {
        return bar!i();
    }

won't work, and neither will

    auto foo(immutable int i = 42)
    {
        return bar!i();
    }

And allowing

    immutable i = foo();
    int[i] arr;

to work doesn't even buy us anything. You could simply use enum instead of
immutable, and it works perfectly fine.

My guess is that the current behavior was added because of an enhancement
request, and the person who implemented it didn't think of all of the
consequences that result from that decision (including the issue with __ctfe).
But I definitely think that we'd be better off if you _had_ to use an enum in
this case and that the fact that a variable was const or immutable would have
zero effect on CTFE.

--


More information about the Digitalmars-d-bugs mailing list