www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 7912] New: Cannot read compile variable at compile time

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7912

           Summary: Cannot read compile variable at compile time
           Product: D
           Version: D2
          Platform: x86
        OS/Version: Windows
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: jazeik gmail.com



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. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Apr 14 2012
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7912


Matt Peterson <revcompgeek gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |revcompgeek gmail.com



PDT ---
tst is not a compile time variable, it is a runtime variable. This issue is
invalid.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Apr 14 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7912




PDT ---
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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Apr 14 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7912


SomeDude <lovelydear mailmetrash.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |lovelydear mailmetrash.com



PDT ---
Invalid issue ?

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Apr 21 2012
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7912


Don <clugdbug yahoo.com.au> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |clugdbug yahoo.com.au
         Resolution|                            |INVALID


-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Apr 23 2012