I want a module level initialised delegate. if I try

module foo;
enum Status
{
     success,
}

class StatusException : Exception
{
     Status s;
     // usual exception constructors
}
void delegate(Status) onError = (Status s) { throw new StatusException(s);};

I get a error like cannot initialise something that needs a context at compile time. Ignoring the fact the the initialiser does not need a context, if I do

void delegate(Status) onError;
static this()
{
    onError = (Status s) { throw new StatusException(s);};
}

void main()
{
    import std.stdio;
    writeln(onError.funcptr); // null
}

WAT?

void delegate(Status) onError;
static this()
{
    import core.stdc.stdio;
    printf("initialising onError");
    onError = (Status s) { throw new StatusException(s);};
printf("initialising onError: funcptr = 0x%x", onError.funcptr);
}

void main()
{
    import std.stdio;
    writeln(onError.funcptr);
}

output:
null

No "initialising onError", the static this is not even being run!
I'm using LDC master.

See also https://github.com/libmir/dcompute/issues/32

Reply via email to