Equivalent of C++ function-scope static initialization

Mark Isaacson via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Mar 2 14:06:53 PST 2015


I'm looking for the D equivalent of:

//C++
void foo() {
   static string bar = painfulToInitialize(); //Always returns the 
same value
   /* A bunch of code */
}

I don't need the thread-safety that C++ provides in that case, 
though it wouldn't hurt. I'm essentially trying to memoize the 
result of painfulToInitialize() between calls to foo. I could use 
std.functional.memoize, but that seems like a syntactic burden.

Unfortunately, I cannot compute the value at compile time.

What's the idiomatic way of doing something like this?


More information about the Digitalmars-d-learn mailing list