Created
July 29, 2012 07:49
-
-
Save JakobOvrum/3196553 to your computer and use it in GitHub Desktop.
initOnce function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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