thisExePath purity
crimaniak via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Tue Sep 20 12:42:10 PDT 2016
On Tuesday, 20 September 2016 at 13:35:27 UTC, Steven
Schveighoffer wrote:
> Yes, but if your code does instantiate it, it is called, even
> if you don't ever call the function that calls it.
Yes, it's not ideal but better then just global variable and
static block - it's called in any case, even if variable is not
used at all.
Ideal solution will be something like attribute for static block
leading to make it optional, so module will not be included if no
usage of other symbols found. But I don't know way how to make it
so template is used.
> Note that if you don't import the module that contains the
> static ctor, it should be trimmed by the linker.
Let's imagine linker can trim even imported module with static
ctor, if we have something like:
immutable string executablePath;
@local shared static this()
{
import std.file : thisExePath;
executablePath = thisExePath();
}
and there is no references to executablePath. Here it would be
useful, I think. Attribute @local (or @module? the name does not
matter) mean this block used only to init other symbols in this
module so it can be skipped if no references.
> I would absolutely caution you from putting static this()
> inside any template. Unfortunately, due to the way D generates
> these static constructors, any module that uses staticMemoize,
> or *imports a module that uses it*, will be marked as having a
> static constructor, and will potentially create cycles.
Please be more detail about cycles. Do you mean something like
this? https://isocpp.org/wiki/faq/ctors#static-init-order
More information about the Digitalmars-d-learn
mailing list