Program level variables

2022-04-19 Thread PMunch
The [global](https://nim-lang.org/docs/manual.html#pragmas-global-pragma) pragma can also be used for similar behavior to static class members in C++.

Program level variables

2022-04-19 Thread shirleyquirk
If you mean a static member sure you can emulate that: type Foo* = object var fooBar = 9 proc bar*(t: typedesc[Foo]):int = fooBar echo Foo.bar Run with the global unexported and the getter exported, and ufcs

Program level variables

2022-04-19 Thread archnim
> you are referring to > Exactly. But as you said, it's not a current method in Nim.

Program level variables

2022-04-19 Thread Stefan_Salewski
You declare it at global scope, and attach the export marker, the asterisk * to it. But It will not be available automatically everywhere, you still have to import that symbol into all the modules that will use it.

Program level variables

2022-04-19 Thread archnim
By the way, I can bind a proc to a type, can I do the same thing with a var ? (an equivalent of static attributes in C++)

Program level variables

2022-04-19 Thread Stefan_Salewski
> I can bind a proc to a type, Sorry, I can not understand your question that well, maybe Mr. Munch can answer it, he is generally really good in understanding questions. I guess with "bind a proc to a type" you are referring to

Program level variables

2022-04-19 Thread archnim
I want it to share state in real time between several modules, some of which launch threads. Anyway Importing a module containing the globals works fine in my case. I just wanted to know if there is an easier way that I ignored. Thanks.

Program level variables

2022-04-19 Thread PMunch
You can always but an exported global in a module and just import that everywhere. Otherwise you have to use a define I think, or patch it into the system module.. Maybe more important is _why_ you want a program level global.

Program level variables

2022-04-19 Thread archnim
OK. Thank you. I'm reading your book. What a pleasure ! Thank you very much for it.

Program level variables

2022-04-19 Thread archnim
Hello world. How can I create a variable (or constant) that is available across all the modules of a same program ? Is there an equivalent of Nodejs's `Global` object ?