Is this a desing rationale? (static array object member)

Brian Hsu brianhsu.hsu at gmail.com
Sun Sep 30 02:36:35 PDT 2007


Janice Caron Wrote:

> On 9/30/07, janderson <askme at me.com> wrote:
> > sections like [1,1,1,1,1] would be marked as const somehow in the GC (or
> > somewhere).  If a modification occurs it would make a call to dup the
> > first time.
> 
> This is all solved in D2.0, so really it's a non-issue.

Here is my experiment result using DMD 2.004, it seems doesn't mark array literal constant default now, hope it will in future version.

import std.stdio;

class Test
{
    //OK, all class instance.x point to same array.
    int [] x = [1,1,1,1,1]; 

    void addByOne () {
        foreach (int i, int value; x) {
            x[i]++;
        }
    }
}

void main ()
{
    int [] x = [1,1,1,1,1];         // Compile OK
    const(int) [] y = [1,1,1,1,1];  // Compile OK

    // Coimpile Error
    // int [] z = [1,1,1,1,1].dup;
    int [] z = ([1,1,1,1,1]).dup;

    // Compile Error, can't do implicitly converting
    Test a = new Test();
    Test b = new Test();

    a.addByOne();

    // 2,2,2,2,2
    foreach (int value; b.x) {
        writef ("%d ", value);
    }
    writefln();
}


   



More information about the Digitalmars-d mailing list