Order of static this() execution?

Johan Engelen j at j.nl
Sun Feb 23 14:36:56 UTC 2020


On Sunday, 23 February 2020 at 11:55:11 UTC, drathier wrote:
> On Sunday, 23 February 2020 at 11:41:25 UTC, Johan Engelen 
> wrote:
>> On Sunday, 23 February 2020 at 09:59:45 UTC, drathier wrote:
>>> I'm having some trouble with the order in which `static 
>>> this()` runs. This is the order defined in the source file, 
>>> numbered for convenience:
>>
>> To avoid confusion: you have all `static this()` in a single 
>> source file? Or across several source files?
>>
>> -Johan
>
> They're all in a single source file. The `[template]` prints 
> are inside templates,

It's not clear from the language specification, but in this case 
with templates, I am not surprised that the order of execution is 
not the same as in the source file. Probably it does fit with the 
order in the source file if you take into account where the 
template is instantiated in that file (but you shouldn't depend 
on that).
I strongly recommend not to depend on the order of multiple 
static this execution, by either rewriting things or by including 
some logic to make sure things are called in the right order 
(checking that something has already run, and running it if not, 
or cancel running it if it already ran)

-Johan



More information about the Digitalmars-d-learn mailing list