Why it doesn't compile in D 2.0?

Simen kjaeraas simen.kjaras at gmail.com
Sat Jan 16 16:38:36 PST 2010


Lutger <lutger.blijdestijn at gmail.com> wrote:

> Perhaps this is or should be a bug. You can override dup to work in ctfe:
>
> char[] dup(string str)
> {
>      return str.dup;
> }
>
> class Test {
>      string t1 = "test";        //Ok!
>      char[] t2 = "test".dup;    //Compile error
>      char[] t3 = "test".dup();  //Ok!
> }

The problem with this approach is that you now have a pointer to mutable
data, the contents of which are stored in the static data segment, and thus
actually immutable.

Second (and this is related to the first), the pointer will be the same for
all instances, and thus changing the contents of one will change the
contents of all.

IOW, what one (probably) wants in this situation, is a constructor.

-- 
Simen


More information about the Digitalmars-d-learn mailing list