www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - D2.008 const test doesn't work

The following code doesn't do what I expect:

    import std.stdio;

    template constType(T)
    {
        static if(is(T == invariant))
        {
            static const int constType = 3;
        }
        else static if(is(T == const))
        {
            static const int constType = 2;
        }
        else
        {
            static const int constType = 1;
        }
    }

    void main()
    {
        writefln(constType!(int));
        writefln(constType!(const(int)));
        writefln(constType!(invariant(int)));
    }

It prints
1
1
1
wheras I would have expected
1
2
3

It seems that the is(T==const) test isn't working.

Back in the days of D2.007, the following used to work:

    template constType(T)
    {
        static if(is(T == invariant(T)))
        {
            static const int constType = 3;
        }
        else static if(is(T == const(T)))
        {
            static const int constType = 2;
        }
        else
        {
            static const int constType = 1;
        }
    }

    void main()
    {
        writefln(constType!(int));
        writefln(constType!(const(int)));
        writefln(constType!(invariant(int)));
    }

which printed
1
2
3

but now that doesn't work either in D2.008.

We need to be able to distinguish int from const(int) from
invariant(int), in order to write classes like

    struct MyArray!(T) { /*...*/ }

    MyArray!(const(int)) a;
Nov 29 2007