Skip to content

Instantly share code, notes, and snippets.

@JakobOvrum
Created July 29, 2012 07:49
Show Gist options
  • Save JakobOvrum/3196553 to your computer and use it in GitHub Desktop.
Save JakobOvrum/3196553 to your computer and use it in GitHub Desktop.
initOnce function
import std.stdio;
void initOnce(alias var)(lazy typeof(var) init)
{
static bool hasInitialised = false;
if(!hasInitialised)
{
var = init();
hasInitialised = true;
}
}
void test()
{
static Object o;
writeln(cast(void*)o);
initOnce!o(new Object);
}
void main()
{
foreach(i; 0 .. 3)
test(); // printed null, 472FD0 and 472FD0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment