February 04, 2017 module specification for variables | ||||
---|---|---|---|---|
| ||||
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. |
February 04, 2017 Re: module specification for variables | ||||
---|---|---|---|---|
| ||||
Posted in reply to Profile Anaysis | 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;
|
Copyright © 1999-2021 by the D Language Foundation