www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - module specification for variables

reply Profile Anaysis <PA gotacha.com> writes:
I'd like to have a global variable in a module but it must be 
accessed by the module name outside of the module.

Sort of like a static variable in a class.

private blocks it completely and public will allow it to be 
imported in to the global scope. Haven't tried protected but I 
assume that is meaningless.

Basically it is a sort of module initialization variable that 
only needs to be set once(usually) outside the module. So I do 
not want it to be imported in to other modules scope in any way, 
ever(there is no need so why create potential issues).

module x;

special int X;  // special = some keyword that makes this work as 
I intend. e.g., "no_import int X;"


module y;

void main()
{
     import x;
     x.X = 3;
     // X = 3; fails.
}


I know you can say that one can used a named import and all that 
but that isn't the point(I want all the other stuff imported as 
it would be)


I could create a static class, for example, and go through 
that... but seems like a hoop to jump through.
Feb 04 2017
parent Mike Parker <aldacron gmail.com> writes:
On Saturday, 4 February 2017 at 09:31:57 UTC, Profile Anaysis 
wrote:

 module x;

 special int X;  // special = some keyword that makes this work 
 as I intend. e.g., "no_import int X;"


 module y;

 void main()
 {
     import x;
     x.X = 3;
     // X = 3; fails.
 }
extern(C) int X;
Feb 04 2017