On Tuesday, 6 February 2018 at 23:03:07 UTC, dekevin wrote:
Hello everyone,
I just ran into the problem, that I need a static variable, where the initialisation code for that variable is only accessible during run-time (since part of the initialisation code will be dynamically linked).


Is there a way to do this in D?

To be a bit more concrete, this is where I have the problem (where ℚ uses GMP, which is dynamically linked):

struct ℚInf {
   ℚ qval;
   immutable static ℚInf zero = ℚInf(0,1);
   this(long num, long den) {
qval = ℚ(num,den); //this initialisation requires dynamically linked code
    }
}

In case anyone is curios, thanks to tgehr i was able to resolve the issue.
For static variables:
struct ℚInf {
    ℚ qval;
    static ℚInf zero = void;
    static this() {
        zero = ℚInf(0,1);
    }
 }

For immutable static variables (a bit hacky since it sidesteps the type system):
struct ℚInf {
    ℚ qval;
    immutable static ℚInf zero = void;
    static this() @trusted {
        import std.conv: emplace;
        emplace!ℚInf(cast(ℚInf*)&zero,ℚInf(0,1));
    }
 }

Reply via email to