On 6/18/18 4:45 AM, Mr.Bingo wrote:
I got tired of waiting for a solution and rolled my own:



static this()
{

     import std.meta, std.stdio;

     // Find all <module>___This functions linked to this module
     auto Iterate()()
     {
         string[string] s;
         void Iterate2(alias m, int depth = 0)()
         {
             static if (depth < 4)
             {
                 static foreach (symbol_name; __traits(allMembers, m))
                 {

                     static if (symbol_name == "object") { }
                    else static if (symbol_name == m.stringof[7..$]~"___This")
                     {
                         s[m.stringof[7..$]] = m.stringof[7..$]~"___This";
                    } else static if (isModule!(__traits(getMember, m, symbol_name)))
                         {
                            mixin("Iterate2!("~symbol_name~", depth + 1);");
                         }
                 }
             }
         }

         mixin("Iterate2!("~.stringof[7..$]~");");

         return s;
     }


     // Call all
     enum fs = Iterate;
     static foreach(k, f; fs)
         mixin(k~"."~f~"();");
}


This code simply finds all <module>___This static functions linked to the module this static constructor is called from and executes them.

This doesn't solve the original problem but lets one execute the static functions to simulate static this. One could hypothetically even add attributes to allow for ordering(which is what the original cyclic redundancy check is suppose to solve but makes things worse since it is an ignorant algorithm).

The algorithm in druntime is correct, I'm sure you mean that the data provided to the algorithm is not detailed enough. This is actually a very tricky thing to get right. What you may end up with is a program that works or doesn't based on your linker order.

This type of method might be more feasible, a sort of static main(). It does not check for static this of aggregates though but gets around the CRC's banket fatal error.

The worst thing to consider is template class/struct static ctors which get called in the *instantiating* module. I always recommend avoiding putting those into your code.

-Steve

Reply via email to