On Monday, 12 February 2018 at 09:58:13 UTC, Alex wrote:
On Monday, 12 February 2018 at 09:37:56 UTC, Simen Kjærås wrote:

Not really, since D doesn't have a concept of an address associated with a type, only with instances of it. So when you use a static array, the address is hard-coded.

--
  Simen

Ok... so the query on ptr on a static is not to be allowed? And the fact I can perform it is a bug?

I'm sorry, I was apparently unclear. When I said 'static array' above, I meant 'static member'.

Since we've been using arrays in our examples, there could be conflation of ideas there. The fact that you can access (and even modify) the static member's .ptr property (as in S.arr.ptr = [1,2,3].ptr), doesn't mean you can change the address of the array itself (the static data in S). This can be shown by writeln(&S.arr), which will not change.

When you call a static method, as the one in this example:

struct S {
    static int[] arr;
    static void foo() { arr[0]++; }
}

unittest {
    S.foo();
}

No pointer is being passed to foo - it's exactly equivalent to this code:

module test;
int[] arr;
void foo() { arr[0]++; }

unittest {
    foo();
}

Likewise, when a non-static method modifies a static member, it doesn't need to look up the address of the static member - its address is hard-coded.

As an example, try this:

struct S {
   static int n1;
   int n2;
}

unittest {
    import std.stdio;
    S s1;
    S s2;
    // These should be equal:
    writeln(&s1.n1);
    writeln(&s2.n1);
    // These should be different:
    writeln(&s1.n2);
    writeln(&s2.n2);
}

--
  Simen

Reply via email to