Static struct?

Rubn where at is.this
Wed Apr 24 21:22:53 UTC 2019


On Wednesday, 24 April 2019 at 21:12:10 UTC, JN wrote:
> I noticed a construct I haven't seen before in D when reading 
> the description for automem - 
> https://github.com/atilaneves/automem
>
>  static struct Point {
>         int x;
>         int y;
>     }
>
>
> What does "static" do in this case? How does it different to a 
> normal struct?

You can access variables in the current scope when in a nested 
scope, static prevents this so that you don't accidentally access 
any of those values as it adds overhead to the struct.

https://run.dlang.io/is/QNumbz

void main() {
     import std.stdio;

     int x;
     struct A {
          void test() { x = 10; }
     }

     static struct B {
         // void test() { x = 20; } // Error
     }

     A a;
     B b;

     a.test();
     // b.test();

     writeln( x );
}


More information about the Digitalmars-d-learn mailing list