D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 7912 - Cannot read compile time variable at compile time
Summary: Cannot read compile time variable at compile time
Status: RESOLVED INVALID
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: x86 Windows
: P2 normal
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2012-04-14 13:53 UTC by Jan
Modified: 2015-06-09 04:41 UTC (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Jan 2012-04-14 13:53:23 UTC
tst.d
----------------------------

import std.stdio;
import std.conv;

mixin template TTT()
{
    static const uint tst;
    static this()
    {
        tst = 0;
    }
}

class T
{
    mixin TTT;
}

void main()
{
    auto n = 0;
    switch(n)
    {
        case T.tst:
            writeln("T.tst: ", T.tst);
            break;
        default:
            writeln("default: ", n);
            break;
    }
}
----------------------------
Run with: 
> dmd -run tst.d

=============================
result:
> tst.d(23): Error: variable tst cannot be read at compile time
> tst.d(23): Error: variable tst cannot be read at compile time
> tst.d(23): Error: case must be a string or an integral constant, not cast(int)tst

=============================
Expected result:

> T.tst: 0

=============================
Additional Information:

Replace "switch(n)" with "switch(n | (T.tst))" to get expected result.
Comment 1 Matt Peterson 2012-04-14 21:01:59 UTC
tst is not a compile time variable, it is a runtime variable. This issue is invalid.
Comment 2 Matt Peterson 2012-04-14 21:09:04 UTC
Jan: If you want tst to be compile time, use enum instead of const, and just do "enum uint tst = 0;" or even "enum tst = 0;". The module constructor (static this() {...}) is called at runtime, and can do a variety of things, including getting input from the console and other things that the compiler can't evaluate at compile time. Since you didn't give a value at the declaration of tst, D requires you to set it in the module constructor as you have done, but it's not a value that can be read at compile time. I believe if you were to have done "static const uint tst = 0;" instead, it would have worked, but using enum instead is a good way to force a variable to be compile time evaluated.
Comment 3 SomeDude 2012-04-21 11:57:38 UTC
Invalid issue ?