www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Constness thrown away in D2.008

reply "Janice Caron" <caron800 googlemail.com> writes:
Sadly, in D2.008, the constness of basic types seems to be "thrown
away" when passed to templates. This means that code which used to
work in D2.007 now doesn't work in D2.008, and for the life of me, I
can't think of a workaround. Walter said that very few code changes
were needed to get D2.007 const to work in D2.008. Well, I can't get
this to work /at all/.

Here's the basic issue

    import std.stdio;

    template isConst(T)
    {
        static if (is(T == const)) static const bool isConst = true;
        else static const bool isConst = false;
    }

    void main()
    {
        writefln(isConst!(const(int)));
    }

prints false. This used to work in D2.007 (with the minor modification
that you'd write (T == const(T)) instead of (T == const). But the old
way doesn't work either in D2.008.

Just in case this is a bug, I've posted about this in digitalmars-d-bugs.

But in case it's not, what code changes do I need to make, to make this work?
Nov 29 2007
parent reply Walter Bright <newshound1 digitalmars.com> writes:
Janice Caron wrote:
 Sadly, in D2.008, the constness of basic types seems to be "thrown
 away" when passed to templates. This means that code which used to
 work in D2.007 now doesn't work in D2.008, and for the life of me, I
 can't think of a workaround. Walter said that very few code changes
 were needed to get D2.007 const to work in D2.008. Well, I can't get
 this to work /at all/.
 
 Here's the basic issue
 
     import std.stdio;
 
     template isConst(T)
     {
         static if (is(T == const)) static const bool isConst = true;
         else static const bool isConst = false;
     }
 
     void main()
     {
         writefln(isConst!(const(int)));
     }
 
 prints false. This used to work in D2.007 (with the minor modification
 that you'd write (T == const(T)) instead of (T == const). But the old
 way doesn't work either in D2.008.

This was done so that 'ordinary' templates don't get instantiated multiple times for int, const int, and invariant int when that is quite unnecessary for the vast bulk of templates.
 Just in case this is a bug, I've posted about this in digitalmars-d-bugs.

It's not a bug.
 But in case it's not, what code changes do I need to make, to make this work?

template isConst(T:T)
Nov 29 2007
parent Jason House <jason.james.house gmail.com> writes:
Walter Bright wrote:

 Janice Caron wrote:
 Sadly, in D2.008, the constness of basic types seems to be "thrown
 away" when passed to templates. This means that code which used to
 work in D2.007 now doesn't work in D2.008, and for the life of me, I
 can't think of a workaround. Walter said that very few code changes
 were needed to get D2.007 const to work in D2.008. Well, I can't get
 this to work /at all/.
 
 Here's the basic issue
 
     import std.stdio;
 
     template isConst(T)
     {
         static if (is(T == const)) static const bool isConst = true;
         else static const bool isConst = false;
     }
 
     void main()
     {
         writefln(isConst!(const(int)));
     }
 
 prints false. This used to work in D2.007 (with the minor modification
 that you'd write (T == const(T)) instead of (T == const). But the old
 way doesn't work either in D2.008.

This was done so that 'ordinary' templates don't get instantiated multiple times for int, const int, and invariant int when that is quite unnecessary for the vast bulk of templates.
 Just in case this is a bug, I've posted about this in digitalmars-d-bugs.

It's not a bug.
 But in case it's not, what code changes do I need to make, to make this
 work?

template isConst(T:T)

This is not obvious from the docs that this functionality would be different. Is it possible to clarify them?
Nov 29 2007