thisExePath purity
Steven Schveighoffer via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Wed Sep 21 11:40:26 PDT 2016
On 9/20/16 3:42 PM, crimaniak wrote:
> On Tuesday, 20 September 2016 at 13:35:27 UTC, Steven Schveighoffer wrote:
>> 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.
But if this is all that is in the module, why import the module if you
aren't going to use any of it?
>> 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
Sort of, I mean this:
https://dlang.org/spec/module.html#order_of_static_ctor
In other words, because the template has a static ctor in it, just
instantiating it in your unrelated module puts static ctor in your
module. And then cycles can appear that you wouldn't expect to happen.
-Steve
More information about the Digitalmars-d-learn
mailing list