[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 11:54:53 UTC 2018


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

--- Comment #2 from David Bennett <davidbennett at bravevision.com> ---
Disclaimer: Just a D user, I hold no decision making power or insight into the
history here.

const and immutable are runtime lvalues that have a known way to get "a" value
at compiletime. As you have noticed the runtime and compiletime values could be
different. Here is another example to show the same effect.

---
unittest {
    immutable n = __ctfe ? 1 : 2;
    enum j = n;
    assert(n == j);
}
---

If you wanted to opt-in to making sure you could only use n at runtime you
could make it a `static immutable` but this has the effect of running the
assignment expression at compiletime.

I believe the current functionality is being used in various projects so I dont
see this being changed without at least a deprecation process. For example,
I've seen sending const variables as template parameters more than a few times.

As for my personal opinion on this issue, I believe using const and immutable
at compiletime is useful, it's just that the values could be different that's
confusing.

So I believe the current reasoning goes like this:
    immutable values are theoretically known at compile time so why not use
them.
    It's not always possible to ctfe so runtime immutable is assigned at
runtime.
    But the runtime value is not known at compiletime, so when it's used we do
the ctfe then and error if it cant.

--


More information about the Digitalmars-d-bugs mailing list