Bug 7912 – Cannot read compile time variable at compile time

Status
RESOLVED
Resolution
INVALID
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
x86
OS
Windows
Creation time
2012-04-14T13:53:00Z
Last change time
2015-06-09T04:41:31Z
Assigned to
nobody
Creator
jazeik

Comments

Comment #0 by jazeik — 2012-04-14T13:53:23Z
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 by ricochet1k — 2012-04-14T21:01:59Z
tst is not a compile time variable, it is a runtime variable. This issue is invalid.
Comment #2 by ricochet1k — 2012-04-14T21:09:04Z
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 by lovelydear — 2012-04-21T11:57:38Z
Invalid issue ?