September 20, 2016 Re: thisExePath purity | ||||
---|---|---|---|---|
| ||||
Posted in reply to Marc Schütz | On Tuesday, 20 September 2016 at 09:14:39 UTC, Marc Schütz wrote:
> Have a look at `std.concurrency.initOnce`:
> https://dlang.org/phobos/std_concurrency.html#.initOnce
>
> But you will still need to use assumePure() for calling `thisExePath`, and it might do other things that are impure...
Yes, it's near but in this case I try to fix purity, so any variants of lazy initialization is not applicable here.
|
September 20, 2016 Re: thisExePath purity | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | 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 |
September 21, 2016 Re: thisExePath purity | ||||
---|---|---|---|---|
| ||||
Posted in reply to crimaniak | 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 |
Copyright © 1999-2021 by the D Language Foundation