Hi and thanks all!
On Tuesday, 20 September 2016 at 00:43:10 UTC, Jonathan M Davis
wrote:
immutable string executablePath;
shared static this()
{
import std.file : thisExePath();
executablePath = thisExePath();
}
This code is good for my needs but I start to think about how to
call thisExePath only if it is really used and come to this
solution:
import std.traits: ReturnType, Parameters;
string staticMemoize(alias T, Parms = Parameters!T)() pure
{
struct Holder(alias T)
{
static shared immutable ReturnType!T value;
shared static this(){ value = T(Parms); }
}
return Holder!T.value;
}
unittest
{
import std.file : thisExePath;
assert(staticMemoize!thisExePath == thisExePath);
}
Something like this. Need to refine about input parameters, but I
hope, idea is clear.
Unlike the function memoize from phobos staticMemoize really
pure. And unlike proposed solution with ordinary variable
staticMemoize is lazy, because no call - no instantiation.
https://dlang.org/library/std/functional/memoize.html