www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - static assignment

reply Igor <Vladamir.I google.com> writes:
how can I have a static assignment


	static bool isStarted = false;
	public static Application Start(string Name, void 
function(Application) entryPoint)
	{
		if (isStarted)
			assert("Can only call Start once");
                 isStarted = true;
         ...

but I don't want this a runtime check. I want to know at compile 
time. Start should never be used more than once in the entire 
program. It's only used to get control from windows startup. It's 
only called in the static this() of the main module.

changing stuff to static if and static assert doesn't help. I 
tried enum, const, etc but all prevent setting isStarted to true.
Jan 25
parent Steven Schveighoffer <schveiguy yahoo.com> writes:
On 1/25/16 8:40 PM, Igor wrote:
 how can I have a static assignment


      static bool isStarted = false;
      public static Application Start(string Name, void
 function(Application) entryPoint)
      {
          if (isStarted)
              assert("Can only call Start once");
                  isStarted = true;
          ...

 but I don't want this a runtime check.
I'm not sure you even need to worry about this check at all. Just don't call the function again! If you don't want other modules to have access, make the function private in its own module. Then nothing else can call it. Of course, you have to either name it properly or have the startup code in the same module (I'm unsure how Windows does this). But I think if you want the check, it necessarily must be during runtime. At compile time, the code isn't running, it's just compiling. -Steve
Jan 25